Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request getting timed out when using parameter encoding JSONEncoding.default

I am using Alamofire for carrying out all network related requests in my app. I am facing problem while encoding parameter as JSON in get request.

Following in my request:

 Alamofire.request(url, method: .get, parameters: params, encoding: JSONEncoding.default)
 .responseJSON(completionHandler: { (response) in
     switch response.result {
     case .success(let retrivedResult):
         print(retrivedResult)
//         success(brandTags)
         break
     case .failure(let errorGiven):
         print(errorGiven)
         print(String(data: response.data!, encoding: String.Encoding.utf8) ?? "")
         failure(APICaller.parseErrorAndGiveMessage(givenError: errorGiven as NSError))
         break
     }
 })

When I encode the parameters as JSONEncoding.default as above, the request always times out with following in my logs:

2016-12-27 12:22:41.425948 xyz[5140:133008] [] nw_endpoint_flow_service_writes [2.1 35.164.98.40:80 ready socket-flow (satisfied)] Write request has 4294967295 frame count, 0 byte count
2016-12-27 12:23:41.485534 xyz[5140:133041] [] nw_endpoint_flow_service_writes [2.1 35.164.98.40:80 ready socket-flow (satisfied)] Write request has 4294967295 frame count, 0 byte count

Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={NSUnderlyingError=0x60000024a9b0 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=http://xyz-beta.abc.com/v1/brands/1a1/notifications, NSErrorFailingURLKey=http://xyz-beta.abc.com/v1/brands/1a1/notifications, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-2102, NSLocalizedDescription=The request timed out.}
2016-12-27 12:23:41.488336 xyz[5140:133868] [] __tcp_connection_write_eof_block_invoke Write close callback received error: [89] Operation canceled

But when I remove the parameter encoding like below, the request completes properly without any problem.

Alamofire.request(url, method: .get, parameters: params, encoding: JSONEncoding.default)
     .responseJSON(completionHandler: { (response) in
         switch response.result {
         case .success(let retrivedResult):
             print(retrivedResult)
    //         success(brandTags)
             break
         case .failure(let errorGiven):
             print(errorGiven)
             print(String(data: response.data!, encoding: String.Encoding.utf8) ?? "")
             failure(APICaller.parseErrorAndGiveMessage(givenError: errorGiven as NSError))
             break
         }
     })

What is the making the difference?

UPDATE:

I opened this issue with Alamofire community on Github and this is their response. Hope this helps people who faced similar issue.

like image 737
Rohan Sanap Avatar asked Dec 27 '16 07:12

Rohan Sanap


1 Answers

So according to the Alamofire community on the issue I opened on GitHub for my above question, they suggest that this is a very common behaviour seen many times and the solution to this is URLEncoding.queryString encode the parameters in the GET request because some servers don't like bodyData in GET request.

So essentially my request code was modified like this:

Alamofire.request(url, method: .get, parameters: params, encoding: URLEncoding.queryString)
 .responseJSON(completionHandler: { (response) in
     switch response.result {
     case .success(let retrivedResult):
         print(retrivedResult)
//         success(brandTags)
         break
     case .failure(let errorGiven):
         print(errorGiven)
         print(String(data: response.data!, encoding: String.Encoding.utf8) ?? "")
         failure(APICaller.parseErrorAndGiveMessage(givenError: errorGiven as NSError))
         break
     }
 })

And this perfectly worked for me.

like image 192
Rohan Sanap Avatar answered Oct 22 '22 11:10

Rohan Sanap