Alamofire: Sending GET with JSON in body gets Timeout error
// in Router: URLRequestConvertible method asURLRequest()
var urlRequest = URLRequest(url: someURL)
urlRequest.httpMethod = HTTPMethod.get.rawValue
let params: Parameters = [
"location": [12.12, 12.12]
]
urlRequest = try JSONEncoding.default.encode(urlRequest, with: params)
return urlRequest
When I try Alamofire.request(Router.someCase), it does not work. I tested all my routers, and JSON encoding with routers work fine with POST requests. I don’t know if anyone else is having this problem.
This is part of the error log until the request times out:
nw_host_stats_add_src recv too small, received 24, expected 28
nw_endpoint_flow_service_writes [2.1 ::1.8081 ready socket-flow (satisfied)] Write request has 4294967295 frame count, 0 byte count
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 4
- Comments: 20 (3 by maintainers)
I figured out my problem. In my Router, I had my GET requests using JSONEncoding:
urlRequest = try JSONEncoding.default.encode(urlRequest, with: parameters)
To fix it, I changed it to URLEncoding:urlRequest = try URLEncoding.default.encode(urlRequest, with: parameters)
@willc0de4food that works!
GET
requests don’t have a body, if you are using a body in yourGET
request you are not respecting HTTP 1.1 Specs: https://tools.ietf.org/html/rfc2616#section-4.3 https://tools.ietf.org/html/rfc2616#section-9.3Thus, any data in a
GET
should be passed in the Headers or URL Params. So to fix this, all I changed was the encoding:to
UPDATE: I tried the same with
NSMutableURLRequest
And it time outed as well… 😦
You need to figure out what your server expects for a parameter encoding during a
GET
and use the includedParameterEncoding
type to encode your requests, or write your own if none of our provided encoders meet those requirements.Also, if you all could post what types of servers you’re connecting to, it would be interesting to see what servers/configurations can’t handle body data in
GET
s.Just a correction: After further inspection of the second TCP dump - URLEncoding actually stuck the parameters into the url as url parameters - not a body. However this does not change the fact that POSTs send the body while other GET methods do not.
@jshier Update: Definitely is not a server problem. My logs show that no GET requests with embedded JSON even hit the server. I tried another pattern for embedding JSON to no avail
Hope to get feedback soon.
Did you guys fix it? It took me so long before realizing it could be an Alamofire error.