我通过邮递员得到了api的回复
{
"status": "1",
"error": false,
"message": "Your order has been placed successfully"
}
我打电话给api并通过需要调用api的params。代码给出错误“字符1周围的值无效”。
let urlsContainer = UrlsContainer()
let url = URL(string: urlsContainer.allotRunnerAPI)
let session = URLSession.shared
var request = URLRequest(url: url!)
let postString = "user_id=\(user_id)&pincode=\(pincode)&select_address=\(select_address)&store_id=\(store_id)"
request.httpMethod = "POST"
request.httpBody = postString.data(using: .utf8)
request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
let task = session.dataTask(with: request as URLRequest, completionHandler: { data, response, error in
guard error == nil else {
return
}
guard let data = data else {
return
}
代码应该执行do代码块
do {
let parsedData = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as! [String: AnyObject]
print(parsedData)
}
但现在由于代码中的一些问题,它正在执行catch代码块
catch let error {
print(error)
}
})
task.resume()
我无法在我的代码中找到解决错误的问题
使用[String:String]
将数据发送到api
let params = ["user_id":user_id,
"pincode":pincode,"select_address":select_address,
"store_id":store_id]
let data = JSONSerialization.data(withJSONObject: params, options: .prettyPrinted)
将此数据发布为您的身体