In Spring Framework 6.1 M3, using BufferingClientHttpRequestFactory with JdkClientHttpRequestFactory throws an exception

java.lang.IllegalArgumentException: non-positive contentLength: 0.

The issue seems to be from class BufferingClientHttpResponseWrapper, method

    @Override
    public InputStream getBody() throws IOException {
        if (this.body == null) {
            this.body = StreamUtils.copyToByteArray(this.response.getBody()) 
        }
        return new ByteArrayInputStream(this.body);
    }

Since BufferingClientHttpResponseWrapper is not public, I copied the class and was able to fix the issue with

    @Override
    public InputStream getBody() throws IOException {
        if (this.body == null) {
            this.body = StreamUtils.copyToByteArray((this.response.getBody() != null) ? this.response.getBody() : new ByteArrayInputStream(new byte[0]));
        }
        return new ByteArrayInputStream(this.body);
    }

Not sure if this is a good fix but it works.

Comment From: sbrannen

The contract of org.springframework.http.HttpInputMessage.getBody() states that it can never return null.

So, in theory, this.response.getBody() should never be null.

Can you provide a minimal example that demonstrates the behavior you've described?

Also, a complete stack trace would be helpful.

Comment From: poutsma

Indeed, like @sbrannen said, HttpInputMesage::getBody should not return null. And looking at the JdkClientHttpResponse class, you can see that we specifically guard against a null body stream: https://github.com/spring-projects/spring-framework/blob/cf75a0901132adf2c65b8ea1f7ead2b0d80875cf/spring-web/src/main/java/org/springframework/http/client/JdkClientHttpResponse.java#L55

Comment From: spring-projects-issues

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

Comment From: spring-projects-issues

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.