简体   繁体   English

Swift 5 & Alamofire 5:GET 方法错误:Alamofire.AFError.URLRequestValidationFailureReason.bodyDataInGETRequest(22 bytes)

[英]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.

相关问题 Swift 5.1 &amp; Alamofire 5.1:获取方法错误 - Swift 5.1 & Alamofire 5.1 : GET method ERROR Alamofire+Combine:如何从 AFError 中获取自定义错误类型 - Alamofire+Combine: how to get custom error type out of AFError 错误:可选(Alamofire.AFError.invalidURL(“”)) - Error: Optional(Alamofire.AFError.invalidURL(“”)) Alamofire:responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength) - Alamofire: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength) “结果”类型的 Alamofire 值<any, aferror> ' 没有成员 'isSuccess' (Swift 5)</any,> - Alamofire Value of type 'Result<Any, AFError>' has no member 'isSuccess' (Swift 5) Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(错误域=NSCocoaErrorDomain 代码=3840 - Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 操作无法完成。 (Alamofire.AFError 错误 9。) - The operation could not complete. (Alamofire.AFError error 9.) Alamofire - Alamofire.AFError.responseSerializationFailed - Xcode 8 - Alamofire - Alamofire.AFError.responseSerializationFailed - Xcode 8 在Swift 2中使用Alamofire进行错误处理 - Error Handling with Alamofire in Swift 2 Swift Alamofire:错误-999 - Swift Alamofire: Error - 999
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM