美文网首页
Alamofire.AFError.URLRequestVali

Alamofire.AFError.URLRequestVali

作者: 小小土豆dev | 来源:发表于2020-05-22 18:19 被阅读0次
Alamofire.AFError.URLRequestValidationFailureReason.bodyDataInGETRequest(2 bytes)

从 Alamofire 5 开始,当你尝试发起GET请求时,parameters参数只能传 nil 不能传空([:]),所以再传参的时候需要判断一下请求方式,如果是GET时,parameters传个nil。

class Request: NSObject {
  var url = ""
  var params: [String: Any] = [:]
  var method: HTTPMethod = .get
}
class func execute(request: Request) {
    var paramters: [String: Any]? = nil
    // 判断请求方式,非GET方式
    if request.method != .get {
      paramters = request.params
    }
    let headers: HTTPHeaders = ["Content-Type": "application/json;charset=UTF-8"]
    let encoding: ParameterEncoding = JSONEncoding.default
    AF.request(request.url,
               method: request.method,
               parameters: paramters, // 如果是GET时,parameters传个nil
               encoding: encoding,
               headers: headers)
      .validate(statusCode: 200..<600)
      .responseJSON(queue: DispatchQueue.main,
                    options: JSONSerialization.ReadingOptions.allowFragments) { (response) in
                      var data = response.value
                      print("-----> [Request] - \(request.url)")
                      print("-----> [Response] - \(data != nil ? data as Any : response as Any)")
    }

https://stackoverflow.com/questions/60960976/swift-5-alamofire-5-get-method-error-alamofire-aferror-urlrequestvalidation

相关文章

网友评论

      本文标题:Alamofire.AFError.URLRequestVali

      本文链接:https://www.haomeiwen.com/subject/lwrcahtx.html