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)")
}
网友评论