ios - 在 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???? 谁能帮帮我吗?提前致谢。
解决方案
推荐阅读
- c++ - c++ 将 4bytes 转换为 UTC 时间
- javascript - 如何使用 Javascript 和 PHP 验证引荐来源网址
- python - 在python中将文件路径作为命令行参数传递
- java - Android volley gridview onclick 项目索引不以 0 开头
- django-rest-framework - DRF 响应列表字段
- batch-file - Windows cmd (.bat) 中的嵌套运算符
- java-8 - Java8 Stream Collectors - 根据值的总和拆分列表
- python-3.x - 对 Pandas 数据框进行分组并根据条件进行验证
- java - Spring Boot 应用程序创建两个数据库行
- ruby - 比较哈希(减法)的元素 RUBY