Description: I'm using springboot3.2.5 and using RestClient.create() I'm getting an instance request header without content-length, which might be an error on some api service requests. But I'll change it to RestClient.builder() .requestInterceptor((request, body, execution) -> execution.execute(request, body)) After.build() gets the instance, there is content-length in the request header.Is this a bug?
Demo:
RestClient.create()
.post()
.uri("http://127.0.0.1:1930/api/test/haha")
.body(map)
.exchange((clientRequest, clientResponse) -> {
System.out.println(clientRequest.getHeaders());
return "";
});
fix demo
RestClient.builder()
requestInterceptor((request, body, execution) -> execution.execute(request, body))
.build()
.post()
.uri("http://127.0.0.1:1930/api/test/haha")
.body(map)
.exchange((clientRequest, clientResponse) -> {
System.out.println(clientRequest.getHeaders());
return "";
});
Comment From: bclozel
To reduce memory usage in RestClient and RestTemplate, most ClientHttpRequestFactory implementations no longer buffer request bodies before sending them to the server. As a result, for certain content types such as JSON, the contents size is no longer known, and a Content-Length header is no longer set. If you would like to buffer request bodies like before, simply wrap the ClientHttpRequestFactory you are using in a BufferingClientHttpRequestFactory.