Skip to content

Commit 019ce44

Browse files
committed
BufferingClientHttpRequestWrapper should not write empty body
Prior to this commit, `BufferingClientHttpRequestWrapper` would always write to the actual client request body, even if the buffered content was empty (empty byte array). This would cause issues with specific client request factories, especially the OkHttp variant, that would consider empty byte arrays as non-empty body and would reject such cases for GET requests with an "IllegalArgumentException: method GET must not have a request body". This commit only writes to the request if the buffered content is not empty. Fixes gh-32612
1 parent a0136a2 commit 019ce44

File tree

2 files changed

+47
-14
lines changed

2 files changed

+47
-14
lines changed

Diff for: spring-web/src/main/java/org/springframework/http/client/BufferingClientHttpRequestWrapper.java

+16-14
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,23 @@ public URI getURI() {
5555
protected ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException {
5656
this.request.getHeaders().putAll(headers);
5757

58-
if (this.request instanceof StreamingHttpOutputMessage streamingHttpOutputMessage) {
59-
streamingHttpOutputMessage.setBody(new StreamingHttpOutputMessage.Body() {
60-
@Override
61-
public void writeTo(OutputStream outputStream) throws IOException {
62-
StreamUtils.copy(bufferedOutput, outputStream);
63-
}
58+
if (bufferedOutput.length > 0) {
59+
if (this.request instanceof StreamingHttpOutputMessage streamingHttpOutputMessage) {
60+
streamingHttpOutputMessage.setBody(new StreamingHttpOutputMessage.Body() {
61+
@Override
62+
public void writeTo(OutputStream outputStream) throws IOException {
63+
StreamUtils.copy(bufferedOutput, outputStream);
64+
}
6465

65-
@Override
66-
public boolean repeatable() {
67-
return true;
68-
}
69-
});
70-
}
71-
else {
72-
StreamUtils.copy(bufferedOutput, this.request.getBody());
66+
@Override
67+
public boolean repeatable() {
68+
return true;
69+
}
70+
});
71+
}
72+
else {
73+
StreamUtils.copy(bufferedOutput, this.request.getBody());
74+
}
7375
}
7476

7577
ClientHttpResponse response = this.request.execute();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.http.client;
18+
19+
/**
20+
* Tests for {@link BufferingClientHttpRequestWrapper} for clients
21+
* not supporting non-null, empty request bodies for GET requests.
22+
*/
23+
class BufferingClientHttpRequestFactoryWithOkHttpTests extends AbstractHttpRequestFactoryTests {
24+
25+
@Override
26+
@SuppressWarnings("removal")
27+
protected ClientHttpRequestFactory createRequestFactory() {
28+
return new BufferingClientHttpRequestFactory(new OkHttp3ClientHttpRequestFactory());
29+
}
30+
31+
}

0 commit comments

Comments
 (0)