← Back to list

Receiving API Response Headers but Stuck Waiting for the Body? Check the Content-Length Header!

If the Content-Length is longer than the actual body length, the client side can wait for the response continuously as it expects more…

Paul Chuang · 2024-09-19 06:05 · 0 claps · 1.5 min read
#content-length #http-request #http-client #server-timeout #http-headers
Open on Medium ↗

Receiving API Response Headers but Stuck Waiting for the Body? Check the Content-Length Header!

If the Content-Length is longer than the actual body length, the client side can wait for the response continuously as it expects more content to come from the server.

It happened to me that the API client keeps waiting for response from server incessatnly when the server has already responded long ago.

Then I realized it is the Content-Length header that somehow got changed in the server side code, causing it to be longer that the real response body length. The client keeps waiting for something the never comes.

Below I will show how to reproduce the issue.

Normal Case

Normal server side code (in Java SpringBoot)

...
import org.springframework.http.ResponseEntity;

@RestController
public class HelloWorldController {
    @PostMapping("/hello-world")
    public ResponseEntity helloworld() {
        HttpHeaders headers = new HttpHeaders();
        return new ResponseEntity<>("hello, world", headers, HttpStatus.OK);
    }
}

Normal client API call

The length of “hello, world” is 12.

The length of “hello, world” is 12.

Content-Length Too Long

However, if I mannually set the content-length to 50 in the response header,

...
import org.springframework.http.ResponseEntity;

@RestController
public class HelloWorldController {
    @GetMapping("/hello-world")
    public ResponseEntity helloworld() {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentLength(50); // manually set the content length longer
        return new ResponseEntity<>("hello, world", headers, HttpStatus.OK);
    }
}

the client will wait for the rest of the content to be returned, thus causing it to wait continously, possibily until a possible timeout of some sort (client timeout or server side LB timeout).


메타데이터
post_id
b2230f844650
slug
api-client-waits-for-server-response-until-timeout-b2230f844650
url
https://medium.com/@paul-d-chuang/api-client-waits-for-server-response-until-timeout-b2230f844650
canonical_url
https://medium.com/@paul-d-chuang/api-client-waits-for-server-response-until-timeout-b2230f844650
author_url
https://medium.com/@paul-d-chuang
status
ok
fetched_at
2026-07-29 22:47:20