Affects: \


Hello,

In Spring Web 6.1.x, when transmitting data as Multipart Form Data using RestTemplate, the Transfer-Encoding header is always declared as Chunked.

Upon inspecting the code, it's found that in version 6.0.x, there was code in executeInternal() to check the length of the body of the Multipart Form Data. The corresponding entity was declared using ByteArrayEntity(). However, this code is no longer present in 6.1.x.

6.0.x

org.springframework.http.client.HttpComponentsClientHttpRequest.java

    @Override
    protected ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException {
        addHeaders(this.httpRequest, headers);

        ContentType contentType = ContentType.parse(headers.getFirst(HttpHeaders.CONTENT_TYPE));
        HttpEntity requestEntity = new ByteArrayEntity(bufferedOutput, contentType);
        this.httpRequest.setEntity(requestEntity);
        HttpResponse httpResponse = this.httpClient.execute(this.httpRequest, this.httpContext);
        Assert.isInstanceOf(ClassicHttpResponse.class, httpResponse,
                "HttpResponse not an instance of ClassicHttpResponse");
        return new HttpComponentsClientHttpResponse((ClassicHttpResponse) httpResponse);
    }

6.1.x

org.springframework.http.client.HttpComponentsClientHttpRequest.java

    @Override
    protected ClientHttpResponse executeInternal(HttpHeaders headers, @Nullable Body body) throws IOException {
        addHeaders(this.httpRequest, headers);

        if (body != null) {
            HttpEntity requestEntity = new BodyEntity(headers, body);
            this.httpRequest.setEntity(requestEntity);
        }
        ClassicHttpResponse httpResponse = this.httpClient.executeOpen(null, this.httpRequest, this.httpContext);
        return new HttpComponentsClientHttpResponse(httpResponse);
    }

To automatically declare Content-Length instead of Transfer-Encoding Chunked as before, what steps should be taken?

Thank you.

Comment From: snicoll

@petercheon thanks for the report. Please review the upgrade guide, in particular the note on BufferingClientHttpRequestFactory.