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)

Most upvoted comments

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 your GET 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.3

Thus, any data in a GET should be passed in the Headers or URL Params. So to fix this, all I changed was the encoding:

Alamofire.request(url, method: .get, parameters: parameters, encoding: JSONEncoding(), headers: headers)

to

Alamofire.request(url, method: .get, parameters: parameters, encoding: URLEncoding(), headers: headers)

UPDATE: I tried the same with NSMutableURLRequest

let headers = [
            "Content-Type": "application/json",
            "Authorization": "Bearer XXXX",
        ]
        let parameters = ["since": "2019-03-05T11:32:00"] as [String: Any]

        let postData = try! JSONSerialization.data(withJSONObject: parameters, options: [])

        let request = NSMutableURLRequest(url: NSURL(string: "https://api.dev.XXX/api/sync")! as URL,
                                          cachePolicy: .useProtocolCachePolicy,
                                          timeoutInterval: 10.0)
        request.httpMethod = "GET"
        request.allHTTPHeaderFields = headers
        request.httpBody = postData as Data

        let session = URLSession.shared
        let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (_, response, error) -> Void in
            if error != nil {
                print(error)
            } else {
                let httpResponse = response as? HTTPURLResponse
                print(httpResponse)
            }
        })

        dataTask.resume()

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 included ParameterEncoding 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 GETs.

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

urlRequest.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: [])

Hope to get feedback soon.

Did you guys fix it? It took me so long before realizing it could be an Alamofire error.