所以我正在尝试使用 Alamofire 执行 PUT 请求,我需要一个整数参数(不是对象)。 请求向数据库发送一个 id,查询对数据库中具有该 id 的对象进行更新。 alamofire 中的 Parameters 对象似乎只接受对象:
var parameters: Parameters = ["key" : "value"]
有什么方法可以不使用对象只发送一个整数吗? 当我使用上面的方法时,我不断收到的错误是:
nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of int out of START_OBJECT token
我假设这意味着我在应该传递一个 int 而不是传递一个对象。
这是我的要求:
Alamofire.request(url, method: .put, parameters: parameters, encoding: JSONEncoding.default, headers: nil).response{ response in
if response.response?.statusCode == 200 {
// pass
}else{
// fail
}
completionHandler((response.response?.statusCode)!)
}
我似乎无法在网上找到任何与我们有关的示例。
最佳答案
如果您提供有关将请求发送到何处的更多信息,那么我可以测试我的解决方案以查看它是否有效。但你可以试试这个。
let url = "YOUR_URL"
let yourData = "WHATEVER CUSTOM VAL YOU WANT TO PASS"
var request = URLRequest(url: URL(string: url)!)
request.httpMethod = "PUT"
//request.setValue("application/json", forHTTPHeaderField: "Content-Type") //This is if you want to have headers. But it doesn't look like you need them.
request.httpBody = yourData
/*
do {
request.httpBody = try JSONSerialization.data(withJSONObject: yourData)
} catch {
print("JSON serialization failed: \(error)")
}
*/
Alamofire.request(request).responseJSON {(response) in
if response.response?.statusCode == 200 {
print ("pass")
}else{
print ("fail")
}
completionHandler((response.response?.statusCode)!)
/*
switch response.result {
case .success(let value):
print ("success: \(value)")
case .failure(let error):
print("error: \(error)")
}
*/
}
关于swift - 在 Alamofire PUT 请求中传递一个整数作为参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47577977/