所以这是我的情况
我有一个parameters
let body: Parameters = [
"name": "Sample Name"]
我正在使用alamofire
发送这样的主体变量:
Alamofire.request(BASE_URL",method: .post,parameters: body,encoding: JSONEncoding.default).responseData { response in
debugPrint("All Response Info: \(response)")
//printing the body just to see what it is sending and everything is as it should be
print(body)
if let data = response.result.value, let utf8Text = String(data: data, encoding: .utf8) {
print("Data: \(utf8Text)")
}
}
问题是响应是带有错误消息400 bad request
的{{1}}响应。name is required
中需要此响应,但由于某些原因,api
无法正确发送我猜。为什么会这样,我该如何解决?
答案 0 :(得分:0)
根据API文档,您可能希望将编码类型更改为URLEncoding,如下所示:-
URLEncoding.default
答案 1 :(得分:0)
尝试使用responseData
代替responseJSON
Alamofire.request("BASE_URL", method: .post, parameters: body, encoding:
JSONEncoding.default).responseJSON { response in
}