首页 > 解决方案 > 在 HTTP Post Request 中传递多个不同类型的参数会导致 InvalidArgument Exception

问题描述

我需要传递多个 String 和 NSData 类型的参数。所以我将所有细节添加为字典。首先我尝试了postString.percentEncoding。但我抓住了异常。所以我尝试了以下。有了这个我得到了 NSInvalidArgumentException。

func receiptValidation()
    {

             let SUBSCRIPTION_SECRET = mySecretKey
         let defaults = UserDefaults.standard
             let receiptPath = Bundle.main.appStoreReceiptURL?.path
             if FileManager.default.fileExists(atPath: receiptPath!){
                 var receiptData:NSData?
                 do{
                     receiptData = try NSData(contentsOf: Bundle.main.appStoreReceiptURL!, options: NSData.ReadingOptions.alwaysMapped)
                 }
                 catch{
                     print("ERROR: " + error.localizedDescription)
                 }

                 let base64encodedReceipt = receiptData?.base64EncodedString(options: NSData.Base64EncodingOptions.endLineWithCarriageReturn)

                 print(base64encodedReceipt!)


                let requestDictionary = ["receipt-data":base64encodedReceipt!,"password":SUBSCRIPTION_SECRET]

                 guard JSONSerialization.isValidJSONObject(requestDictionary) else {  print("requestDictionary is not valid JSON");  return }
                 do {
                     let requestData = try JSONSerialization.data(withJSONObject: requestDictionary)


                    let URLForApplication:String = String(format:"%@/api/validate-receipt-data",opcodeDetails["apiProxyBaseUrl"]!)                         SwiftyBeaver.info("URLForApplication Path:\n\(URLForApplication)")
                     let url:URL! = URL.init(string: URLForApplication)
                    var request = URLRequest.init(url: url)
                     request.httpMethod = "POST"
                    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
                    let configure = URLSessionConfiguration.background(withIdentifier: Bundle.main.bundleIdentifier!)

                    var postString:[String:Any]=[
                        "receiptData":requestData,
                        "deviceType":"IOS",
                        "subscriberId":encodeString(normalString: defaults.array(forKey: "userDetails")?.first as! String),
                        "password":encodeString(normalString: defaults.array(forKey: "userDetails")?.last as! String),
                        "productId":PurchaseManager.instance.productId,

                        "code":opcodeDetails["opCode"]!
                    ]
                     do {
                           request.httpBody = try JSONSerialization.data(withJSONObject: postString, options: .prettyPrinted)                       
 } catch let error {
                           print(error.localizedDescription)
                       }

                     let task = session1?.uploadTask(with: request, from: requestData) { (data, response, error) in
                         if let data = data , error == nil {
                             do {
                                 let appReceiptJSON = try JSONSerialization.jsonObject(with: data)
                                 print("success. here is the json representation of the app receipt: \(appReceiptJSON)")

                             } catch let error as NSError {
                                 print("json serialization failed with error: \(error)")
                             }
                         } else {
                             print("the upload task returned an error: \(error)")
                         }
                     }
                    task?.resume()
                 } catch let error as NSError {
                     print("json serialization failed with error: \(error)")
                 }



             }

    }

为什么我收到 NSInvalidArgumenException???? 谁能帮帮我吗?提前致谢。

标签: iosjsonswiftxcodeexception

解决方案


推荐阅读