[英]Swift 5 & Alamofire 5 : GET method ERROR: Alamofire.AFError.URLRequestValidationFailureReason.bodyDataInGETRequest(22 bytes)
I am trying to get records from Database using Alamofire.我正在尝试使用 Alamofire 从数据库中获取记录。 I am sending parameters in GET request as below.我在 GET 请求中发送参数如下。
let headers : HTTPHeaders = ["x-access-token": "\(t)","username":"\(Base.sharedManager.user)","password":"\(Base.sharedManager.pass)"]
let parm : [String: Any] = ["search_str" : self!.searchStr]
// let searchUrl = Base.sharedManager.URL+"questions/get/"+self!.searchStr
let searchUrl = Base.sharedManager.URL+"questions/get/"
AF.request(searchUrl, method: .get, parameters: parm, encoding:JSONEncoding.default , headers: headers, interceptor: nil).response { (responseData) in
guard let data = responseData.data else {
debugPrint("Error getting question data", responseData.error as Any)
self?.showNoResults()
return
}
do {
let sResults = try JSONDecoder().decode(SearchResults.self, from: data)
self!.searchReturn = [sResults]
self!.qSearchTV.reloadData()
} catch {
self?.showNoResults()
print("Error retriving questions \(error)")
}
}
Got the error below when above code executed: "Error getting question data" Optional(Alamofire.AFError.urlRequestValidationFailed(reason: Alamofire.AFError.URLRequestValidationFailureReason.bodyDataInGETRequest(23 bytes)))执行上述代码时出现以下错误:“获取问题数据时出错” Optional(Alamofire.AFError.urlRequestValidationFailed(reason: Alamofire.AFError.URLRequestValidationFailureReason.bodyDataInGETRequest(23 bytes)))
Use URLEncoding.default
instead of JSONEncoding.default
使用URLEncoding.default
而不是JSONEncoding.default
AF.request(path,
method: .get,
parameters: params,
encoding: URLEncoding.default,
headers: nil)
.response { (responseData) in
}
Alamofire 5 and Apple's 2019 frameworks now produce an error when you try to make a GET
request with body data, as such a request is invalid.当您尝试使用正文数据发出GET
请求时,Alamofire 5 和 Apple 的 2019 框架现在会产生错误,因为此类请求无效。 I would suggest checking to make sure that's what your server is expecting, and if it does really require body data for GET
requests, reach out to the API provider and request a change, as no device running Apple's 2019 OSes will be able to make such a request.我建议检查以确保这是您的服务器所期望的,如果它确实需要GET
请求的正文数据,请联系 API 提供商并请求更改,因为运行 Apple 2019 操作系统的任何设备都无法做到这一点一个要求。
You have to remove the "parameters" parameter.您必须删除“参数”参数。
Instead of doing this:而不是这样做:
AF.request("https://httpbin.org/get",
method: .get,
parameters: [:],
encoding: URLEncoding.httpBody,
headers: [:])
Do this:做这个:
AF.request("https://httpbin.org/get",
method: .get,
encoding: URLEncoding.httpBody,
headers: [:])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.