如何使用 Alamofire 在 Swift 5 中发出包含 "x-www-form-urlencoded" 内容的 POST 请求?
How do I make a POST request with "x-www-form-urlencoded" content in Swift 5 using Alamofire?
我正在尝试从 post 请求中解析 JSON,但我需要将内容作为 x-www-form-urlencoded 传递。我正在使用 Alamofire 和 Swift5,用于执行请求的代码是这样的:
public func performRequest(url: String, parameters: [String:String]){
AF.request(url, method: .post, parameters: parameters, encoding: URLEncoding.httpBody).validate().responseJSON{ response in
switch response.result {
case .success(let JSON): // memorizza il file JSON di risposta in una variabile di nome JSON
print("Validation Successful with response JSON \(JSON)")
case .failure(let error):
// print(error.localizedDescription)
print("Request failed with error \(error)")
}
}
}
如何将内容作为 x-www-form-urlencoded 传递?
您可以通过添加 header 来实现它,例如:
let param:Parameters=[
"param":param]
let headers:HTTPHeaders=[
"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"]
Alamofire.request(url,method: .post,parameters: param,encoding: URLEncoding.httpBody, headers: headers).responseJSON{response in
....
}
我正在尝试从 post 请求中解析 JSON,但我需要将内容作为 x-www-form-urlencoded 传递。我正在使用 Alamofire 和 Swift5,用于执行请求的代码是这样的:
public func performRequest(url: String, parameters: [String:String]){
AF.request(url, method: .post, parameters: parameters, encoding: URLEncoding.httpBody).validate().responseJSON{ response in
switch response.result {
case .success(let JSON): // memorizza il file JSON di risposta in una variabile di nome JSON
print("Validation Successful with response JSON \(JSON)")
case .failure(let error):
// print(error.localizedDescription)
print("Request failed with error \(error)")
}
}
}
如何将内容作为 x-www-form-urlencoded 传递?
您可以通过添加 header 来实现它,例如:
let param:Parameters=[
"param":param]
let headers:HTTPHeaders=[
"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"]
Alamofire.request(url,method: .post,parameters: param,encoding: URLEncoding.httpBody, headers: headers).responseJSON{response in
....
}