Request timeout when using JSONEncoding.default parameter encoding

I use Alamofire to fulfill all network requests in my application. I ran into a problem when setting the encoding as JSON in the 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 disconnects with the 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 delete the parameter encoding as shown below, the request completes correctly without any problems.

 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 difference?

UPDATE:

I discovered this problem with the Alamofire community on Github and this is their answer . Hope this helps people facing this issue.

+6
source share
3 answers

So, according to the Alamofire community on the issue , I opened GitHub for my aforementioned question, they believe that this very general behavior has been observed many times, and the solution to this URLEncoding.queryString encodes the parameters in the GET request, because some servers do not like bodyData in the GET request .

Thus, my request code was changed as follows:

 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 it worked great for me.

+3
source

I just tried and succeeded:

Enter your parameter as [String: AnyObject]

 let params: [String: AnyObject] = ["param1": value1 as AnyObject, "param2": value2 as AnyObject] 

Hope this helps.

0
source

I understood my problem. In my router, I had my GET requests using JSONEncoding:

urlRequest = try JSONEncoding.default.encode (urlRequest, with: parameters)

To fix this, I changed it to URLEncoding:

urlRequest = try URLEncoding.default.encode (urlRequest, with: parameters)

0
source

Source: https://habr.com/ru/post/1013538/


All Articles