Skip to content

Commit 49d5f4d

Browse files
committed
Remove Content-Type Header when request with empty body POST method (Default Client).
- add Content-Length Header with 0 value when `sun.net.http.allowRestrictedHeaders` System Property is set true - fix not running test to run - add README for setting zero content-length header
1 parent 27f6aa6 commit 49d5f4d

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,12 @@ in a context-specific manner -- for example, thread-local storage can be used to
815815
header values depending on the invoking thread, which can be useful for things such as setting
816816
thread-specific trace identifiers for requests.
817817
818+
#### Set zero Content-Length Header
819+
820+
To specify `Content-Length: 0` header when making a request with empty body, system property `sun.net.http.allowRestrictedHeaders` should be set to `true`
821+
822+
If not, the `Content-Length` header will not be added.
823+
818824
### Advanced usage
819825
820826
#### Base Apis

core/src/main/java/feign/Client.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -201,19 +201,14 @@ else if (field.equals(ACCEPT_ENCODING)) {
201201
connection.addRequestProperty("Accept", "*/*");
202202
}
203203

204-
boolean hasEmptyBody = false;
205204
byte[] body = request.body();
206-
if (body == null && request.httpMethod().isWithBody()) {
207-
body = new byte[0];
208-
hasEmptyBody = true;
209-
}
210205

211206
if (body != null) {
212207
/*
213208
* Ignore disableRequestBuffering flag if the empty body was set, to ensure that internal
214209
* retry logic applies to such requests.
215210
*/
216-
if (disableRequestBuffering && !hasEmptyBody) {
211+
if (disableRequestBuffering) {
217212
if (contentLength != null) {
218213
connection.setFixedLengthStreamingMode(contentLength);
219214
} else {
@@ -236,6 +231,12 @@ else if (field.equals(ACCEPT_ENCODING)) {
236231
}
237232
}
238233
}
234+
235+
if (body == null && request.httpMethod().isWithBody()) {
236+
// To use this Header, set 'sun.net.http.allowRestrictedHeaders' property true.
237+
connection.addRequestProperty("Content-Length", "0");
238+
}
239+
239240
return connection;
240241
}
241242

core/src/test/java/feign/client/DefaultClientTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import feign.assertj.MockWebServerAssertions;
2929
import okhttp3.mockwebserver.MockResponse;
3030
import okhttp3.mockwebserver.SocketPolicy;
31+
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
3132

3233
/**
3334
* Tests client-specific behavior, such as ensuring Content-Length is sent when specified.
@@ -79,17 +80,37 @@ public void patch() throws Exception {
7980
assertThat(exception).hasCauseInstanceOf(ProtocolException.class);
8081
}
8182

83+
@Test
8284
@Override
8385
public void noResponseBodyForPost() throws Exception {
8486
super.noResponseBodyForPost();
8587
MockWebServerAssertions.assertThat(server.takeRequest()).hasMethod("POST")
88+
.hasNoHeaderNamed("Content-Type");
89+
}
90+
91+
@Test
92+
@EnabledIfSystemProperty(named = "sun.net.http.allowRestrictedHeaders", matches = "true")
93+
public void noRequestBodyForPostWithAllowRestrictedHeaders() throws Exception {
94+
super.noResponseBodyForPost();
95+
MockWebServerAssertions.assertThat(server.takeRequest()).hasMethod("POST")
96+
.hasNoHeaderNamed("Content-Type")
8697
.hasHeaders(entry("Content-Length", Collections.singletonList("0")));
8798
}
8899

100+
@Test
89101
@Override
90102
public void noResponseBodyForPut() throws Exception {
91103
super.noResponseBodyForPut();
92104
MockWebServerAssertions.assertThat(server.takeRequest()).hasMethod("PUT")
105+
.hasNoHeaderNamed("Content-Type");
106+
}
107+
108+
@Test
109+
@EnabledIfSystemProperty(named = "sun.net.http.allowRestrictedHeaders", matches = "true")
110+
public void noResponseBodyForPutWithAllowRestrictedHeaders() throws Exception {
111+
super.noResponseBodyForPut();
112+
MockWebServerAssertions.assertThat(server.takeRequest()).hasMethod("PUT")
113+
.hasNoHeaderNamed("Content-Type")
93114
.hasHeaders(entry("Content-Length", Collections.singletonList("0")));
94115
}
95116

0 commit comments

Comments
 (0)