【发布时间】:2020-07-13 15:16:39
【问题描述】:
我尝试使用 Alamofire 发出这样的 API 请求:
let param = ["id":"xy", "products":[["quantity":2, "product":["id":123]]]]
Alamofire.request(url, method: .post,
parameters: param, encoding: URLEncoding.default,
headers: ["Accept": "application/json", "Content-Type": "application/json"]).responseJSON ..
我收到了这个回复:
message = "Unexpected token i in JSON at position 0";
statusCode = 400;
我也尝试过这样的请求:
request.httpBody = try! JSONSerialization.data(withJSONObject: param)
我尝试手动执行以下请求以确保它工作正常:
curl -X POST http://url -d'{"id":"xy", "products" [{"quantity":2,"product":{"id":123}}]}' -H'Content-Type: application/json'
根据需要,它给了我这样的回应:
{
"id":"xy",
"products":[
{
"quantity":2,
"product":{
"id":123
}
}
]
}
【问题讨论】:
-
提示:既然你有一个工作卷曲,你知道 Alamofire 可以将它的请求打印为卷曲吗?然后你可以比较和发现问题。见stackoverflow.com/questions/53637437/alamofire-with-d/… 然后就可以看到github.com/Alamofire/Alamofire/blob/master/Source/…放了什么参数,改一下