我现在正在寻找在 Alamofire Content-Type application/json 中为 PUT 方法添加 content-type: application/json
的解决方案。我已经开发如下,但不能正常工作。
Alamofire.request(.PUT, Config.preferenceURL, parameters: param, headers: headers)
.validate(contentType: ["application/json"])
.responseJSON { response in
let swiftyJsonVar = JSON(response.result.value!)
print(swiftyJsonVar)
if (swiftyJsonVar["success"]) {
}
JHProgressHUD.sharedHUD.hide()
}
最佳答案
您需要在request
方法中指定encoding: .JSON
。否则它会将您的参数编码为 URL 中的查询参数。通过使用 .JSON
编码,Content-Type
header 将自动为您设置。
Alamofire.request(.PUT, Config.preferenceURL, parameters: param, encoding: .JSON, headers: headers)
您还可以使用 debugPrint
API 打印出相当于您发送到服务器的请求的 cURL
命令。
let request = Alamofire.request(.PUT, Config.preferenceURL, parameters: param, encoding: .JSON, headers: headers)
debugPrint(request)
干杯。 🍻
关于ios - PUT 方法的 Alamofire Content-Type application/json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38566064/