我正在尝试使用Alamofire将此数据(JSON格式)作为参数发送到swift 3:
[ {"type":"confirm",
"refKey":"123456789",
"quantity": "1"} ]
但我无法将其转换为参数数据,因为接受的数据类型为[String:Any]
如何传递所需的参数?
答案 0 :(得分:2)
我在这篇文章中找到了答案: Send an array as a parameter in a Alamofire POST request
使用Alamofire的JSONSerialization和URLRequest以HTTPBody发送数据。
//creates the request
var request = URLRequest(url: try! "https://api.website.com/request".asURL())
//some header examples
request.httpMethod = "POST"
request.setValue("Bearer ACCESS_TOKEN_HERE",
forHTTPHeaderField: "Authorization")
request.setValue("application/json", forHTTPHeaderField: "Accept")
//parameter array
let values = ["value1", "value2", "value3"]
request.httpBody = try! JSONSerialization.data(withJSONObject: values)
//now just use the request with Alamofire
Alamofire.request(request).responseJSON { response in
switch (response.result) {
case .success:
//success code here
case .failure(let error):
//failure code here
}
}
作者:mourodrigo