ios - Swift-4:如何使用带有“Content-Type”的 URLSession 中的参数的 POST 请求获取数据:“application/x-www-form-urlencoded”
问题描述
朋友们,我已经经历了很多例子,这些例子都可以在 SO 虽然我没有收到正确的答案,但我仍然面临使用 URLSession 和 Post 请求并传递参数通过 api 请求获取数据的问题。
首先,我想告诉你,我有什么。一直试到现在...
func requestApiCall(){
let renewal_id = ""
let policy_no = ""
let client_name = ""
let client_id = ""
let product_name = ""
let created_date_from = ""
let created_date_to = ""
let policy_expiry_from = ""
let policy_expiry_to = ""
self.parameters = ["renewal_id":renewal_id,"policy_no":policy_no,"client_name":client_name,"client_id":client_id,"product_name":product_name,"created_date_from":created_date_from,"created_date_to":created_date_to,"policy_expiry_from":policy_expiry_from,"policy_expiry_to":policy_expiry_to]
let config = URLSessionConfiguration.default
config.httpAdditionalHeaders = [
"Accept" : "application/json",
"Content-Type" : "application/x-www-form-urlencoded"
]
let session = URLSession(configuration: config)
let Url = String(format: "http://myapi-url");
let serviceUrl = URL(string: Url)
var request = URLRequest(url: serviceUrl!)
print(request.url!)
request.httpMethod = "POST"
request.timeoutInterval = 60
request.httpBody = try! JSONSerialization.data(withJSONObject: parameters!, options: [])
let task = session.dataTask(with: request as URLRequest, completionHandler: {data, response, error -> Void in
if error == nil{
print(response!)
}
else {
print(error?.localizedDescription as Any)
}
print(response!)
guard let httpResponse = response as? HTTPURLResponse, let receivedData = data
else {
print("error: not a valid http response")
return
}
switch (httpResponse.statusCode)
{
case 200: //The request was fulfilled
let response = NSString (data: receivedData, encoding: String.Encoding.utf8.rawValue)
if response == "SUCCESS"
{
print("Network - HandShaking Successfull...!!!")
}
else{
print("Network - HandShaking is not successfull...!!!")
}
case 400:
print("response-status - 400 : The request had bad syntax or was inherently impossible to be satisfied.")
case 500:
print("\nresponse-status - 500 : Internal Server Error...!!!")
default:
print("response-status - Unknown : Received Response => \(httpResponse.statusCode)")
}
})
task.resume()
}
运行上述函数后,我得到 httpResponse.statusCode = 500
但是当我在邮递员中运行它时,我得到了正确的响应,就像方面一样。
我也尝试通过邮递员生成代码片段......如下......
func postmanSnippetApiCall(){
let headers = [
"Content-Type": "application/x-www-form-urlencoded",
"cache-control": "no-cache",
"Postman-Token": "5d571157-86c5-4eac-ba6d-b00779ae5dbd"
]
let postData = NSMutableData(data: "renewal_id=".data(using: String.Encoding.utf8)!)
postData.append("&policy_no=".data(using: String.Encoding.utf8)!)
postData.append("&client_name=".data(using: String.Encoding.utf8)!)
postData.append("&client_id=".data(using: String.Encoding.utf8)!)
postData.append("&product_name=".data(using: String.Encoding.utf8)!)
postData.append("&created_date_from=".data(using: String.Encoding.utf8)!)
postData.append("&created_date_to=".data(using: String.Encoding.utf8)!)
postData.append("&policy_expiry_from=".data(using: String.Encoding.utf8)!)
postData.append("&policy_expiry_to=".data(using: String.Encoding.utf8)!)
postData.append("&undefined=undefined".data(using: String.Encoding.utf8)!)
let request = NSMutableURLRequest(url: NSURL(string: "http://myapiurl")! as URL,
cachePolicy: .useProtocolCachePolicy,
timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBody = postData as Data
let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
print(error)
} else {
let httpResponse = response as? HTTPURLResponse
print(httpResponse)
}
})
dataTask.resume()
}
但是在邮递员生成的代码片段中,我在这一行收到错误,即request.httpBody = postData as Data
错误是这个:Cannot convert value of type 'NSMutableData' to type 'Data' in coercion
如果我使用第三方库,即Alamofire,那么我可以很容易地获取数据。
Alamofire 代码片段...完美运行..& 给出了正确的响应。
func apiRequestByAlamofire(){
let urlString = "http://myapiurl"
let params: [String: Any]? = ["renewal_id":"","policy_no":"","client_name":"","client_id":"","product_name":"","created_date_from":"","created_date_to":"","policy_expiry_from":"","policy_expiry_to":""]
Alamofire.request(urlString, method: .post, parameters: params).responseJSON { response in
print(response) //Here getting perfect response successfully...!!!
}
}
但我仍然在通过 URLSession 苦苦挣扎......!!!
我仍然怀疑,为什么我在使用 URLSession 时遇到了太多问题。
以上我的疑问的朋友,请我对您的建议持开放态度,并请帮助我理解它。
不知道,我哪里错了。请帮帮我。
解决方案
经过大量搜索和斗争后,我想出了这个解决方案:
guard var components = URLComponents(url: URL(string: "http://example.com")!, resolvingAgainstBaseURL: true)
else { fatalError("Couldn't create URLComponents") }
components.queryItems = params.map { k, v in URLQueryItem(name: k, value: v) }
var request = URLRequest(url: baseUrl.appendingPathComponent(path.rawValue))
request.httpBody = Data(components.query!.utf8)
request.httpMethod = "POST"
“example.com”字面意思就是这样,因为我只是URLComponents
用来对参数进行编码。
推荐阅读
- reactjs - 如何与另一个查询共享缓存的 apollo-client 数据
- snakemake - Snakemake:目标规则可能不包含通配符
- regex - Bash:删除目录中的大多数文件
- python - 如何将数据添加到数据库并通过计算显示在管理页面上
- angularjs - 查询参数中的编码斜杠 (%2F) 导致 Angular 应用程序中的重定向失败
- graphql-js - 意外
在使用 graphql 时 - python - 具有不可序列化类的多线程多处理
- angular - 当一个 npm 包不是想要的包时,我应该如何/应该更新它?
- java - Java ImageIO.read(未知来源)
- cloudkit - CloudKit CKShare userDidAcceptCloudKitShareWith 在 Mac 应用上从不触发