我正在使用Alamofire
调用外部API上的 .put 方法。在邮递员中,我可以按以下格式成功传递原始JSON
数据,其中每个项目都包含一个ID和一个数量。 :
[{
"id":"176",
"quantity":"2"
}, {
"id":"178",
"quantity":"1"
}]
cURL示例:
PUT /cart HTTP/1.1
Host: someapi.biz
x-api-key: somekey
x-auth-token: sometoken
Content-Type: application/json
cache-control: no-cache
Postman-Token: sometoken
[{
"id":"176",
"quantity":"2"
}, {
"id":"178",
"quantity":"1"
}]------WebKitFormBoundary7MA4YWxkTrZu0gW--
我无法迅速找出如何将该信息正确格式化为Alamofire的一组参数。
for item in OrderedItems {
let rowItem: JSON = ["id" : item.ID, "quantity" : item.Quantity]
??
}
答案 0 :(得分:2)
检查以下代码:
var arrParam = [Any]()
for item in OrderedItems
{
let rowItem: JSON = ["id" : item.ID, "quantity" : item.Quantity]
arrTemp.append(rowItem)
}
// Convert Array into JSON String (Raw)
guard let data = try? JSONSerialization.data(withJSONObject: arrParam, options: []) else {
return
}
let paramString = String(data: data, encoding: String.Encoding.utf8)
var request = URLRequest(url: URL(string: "URL")!)
request.httpMethod = HTTPMethod.put.rawValue
request.httpBody = paramString?.data(using: .utf8)
Alamofire.request(request).responseJSON { (response) in
}
希望这会对您有所帮助。