Alamofire: When receiving a 204 status code, alamofire times out

Created on 7 Jun 2016  ·  3Comments  ·  Source: Alamofire/Alamofire

For a request that i expect a 204 (No Content) to be returned as a status code, Alamofire instead infers this as an error. It returns an error which has the same status code as an request that times out.

The following code was used (very basic usage of Alamofire)

alamofireManager.request(request)
        .validate(statusCode: 200..<400)
        .responseJSON(options: .AllowFragments) { (response: Response<AnyObject, NSError>) in
                handleAlamofireResponse(response)
         }
question response serializer

Most helpful comment

hey @borek2 can you make sure that your api does't send content-length header grater then zero , i had the same issue after digging 3 days in it i found the solution

Problem
the actual problem is that my api sending content-length header with value grater then 0 as response so Alamofire check the content length and he founds that ohh there is content-length grater then zero so i need to read response data so Alamofire going to read it and the server doesn't return anything and there is timeout happened Server doesn't return anything and Alamofire trying to read response

Solution
make sure that api does't return content-length header value grater then 0 when status code is 204

@cnoon we also need to change the logic how Alamofire deal with 204 request code because we are going to read data but case like above dose't have any response data in 204 so we don't have to read data if the response code is 204 no content

Cheers. 🍻

All 3 comments

Hi @borek2,

I just created a test verifying that everything is working as expected.

func testDeleteWith204ResponseStatusCode() {
    let expectation = expectationWithDescription("204 should succeed")

    Alamofire.request(.DELETE, "https://httpbin.org/status/204")
        .validate(statusCode: 200..<400)
        .responseJSON(options: .AllowFragments) { response in
            print(response.response)
            print(response)
            expectation.fulfill()
        }

    waitForExpectationsWithTimeout(timeout, handler: nil)

    print("Yay...204 works as expected with responseJSON serializer")
}

If this does not demonstrate your use case, then please provide a different example that replicates the behavior you're seeing. We have custom 204 handling in the JSON serializer which should ensure you receive a .Success case when using the responseJSON serializer.

I'm going to close this issue out for now. If you provide more info that leads us to believe there may be an issue in Alamofire, we'll happily re-open and investigate further.

Cheers. 🍻

hey @borek2 can you make sure that your api does't send content-length header grater then zero , i had the same issue after digging 3 days in it i found the solution

Problem
the actual problem is that my api sending content-length header with value grater then 0 as response so Alamofire check the content length and he founds that ohh there is content-length grater then zero so i need to read response data so Alamofire going to read it and the server doesn't return anything and there is timeout happened Server doesn't return anything and Alamofire trying to read response

Solution
make sure that api does't return content-length header value grater then 0 when status code is 204

@cnoon we also need to change the logic how Alamofire deal with 204 request code because we are going to read data but case like above dose't have any response data in 204 so we don't have to read data if the response code is 204 no content

Cheers. 🍻

Thanks for the suggestion @indrajitsinh ! After an alamofire update I did not have the problem anymore, but I will remember your suggestion when I am having trouble again. Thanks for replying!

Was this page helpful?
0 / 5 - 0 ratings