Swift 5 & Alamofire 5 : GET method ERROR: Alamofire.AFError.URLRequestValidationFailureReason.bodyDataInGETRequest(22 bytes)

10,145

Solution 1

Use URLEncoding.default instead of JSONEncoding.default

AF.request(path, 
          method: .get, 
      parameters: params, 
        encoding: URLEncoding.default, 
         headers: nil)
  .response { (responseData) in

}

Solution 2

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. 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.

Solution 3

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: [:])
Share:
10,145
Admin
Author by

Admin

Updated on July 22, 2022

Comments

  • Admin
    Admin over 1 year

    I am trying to get records from Database using Alamofire. I am sending parameters in GET request as below.

    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)))

  • Admin
    Admin about 4 years
    Got the point. The specific reason for passing parameter is, i cant send the non English chars thru URL string like “let searchUrl = Base.sharedManager.URL+"questions/get/"+self!.searchStr“. So I changed this to send thru body in get method.
  • Jon Shier
    Jon Shier about 4 years
    You can send non-English characters through the URL just fine, you just need to percent encode them first.
  • Hamed Nova
    Hamed Nova almost 4 years
    may i ask whats the difference betweent these two?
  • gtrujillos
    gtrujillos over 3 years
    @HamedNova, I guess the problem is because the GET method does not have body parameters.
  • user2061097
    user2061097 over 3 years
    @gts why not? I read the docs and you are correct. If you want to use GET, just add URL to search parameters. If you add parameters, it will throw an error.