Skip to content

Commit 7e7940e

Browse files
authored
Merge pull request #2555 from jay-choe/remove-content-type-header-with-empty-post-body
Remove Content-Type Header when request with empty body POST method
2 parents 920ae8c + e2fbea1 commit 7e7940e

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-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
@@ -206,19 +206,14 @@ else if (field.equals(ACCEPT_ENCODING)) {
206206
connection.addRequestProperty("Accept", "*/*");
207207
}
208208

209-
boolean hasEmptyBody = false;
210209
byte[] body = request.body();
211-
if (body == null && request.httpMethod().isWithBody()) {
212-
body = new byte[0];
213-
hasEmptyBody = true;
214-
}
215210

216211
if (body != null) {
217212
/*
218213
* Ignore disableRequestBuffering flag if the empty body was set, to ensure that internal
219214
* retry logic applies to such requests.
220215
*/
221-
if (disableRequestBuffering && !hasEmptyBody) {
216+
if (disableRequestBuffering) {
222217
if (contentLength != null) {
223218
connection.setFixedLengthStreamingMode(contentLength);
224219
} else {
@@ -241,6 +236,12 @@ else if (field.equals(ACCEPT_ENCODING)) {
241236
}
242237
}
243238
}
239+
240+
if (body == null && request.httpMethod().isWithBody()) {
241+
// To use this Header, set 'sun.net.http.allowRestrictedHeaders' property true.
242+
connection.addRequestProperty("Content-Length", "0");
243+
}
244+
244245
return connection;
245246
}
246247

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import okhttp3.mockwebserver.MockResponse;
3232
import okhttp3.mockwebserver.SocketPolicy;
3333
import org.junit.jupiter.api.Test;
34+
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
3435

3536
/** Tests client-specific behavior, such as ensuring Content-Length is sent when specified. */
3637
public class DefaultClientTest extends AbstractClientTest {
@@ -80,19 +81,41 @@ public void patch() throws Exception {
8081
assertThat(exception).hasCauseInstanceOf(ProtocolException.class);
8182
}
8283

84+
@Test
8385
@Override
8486
public void noResponseBodyForPost() throws Exception {
8587
super.noResponseBodyForPost();
8688
MockWebServerAssertions.assertThat(server.takeRequest())
8789
.hasMethod("POST")
90+
.hasNoHeaderNamed("Content-Type");
91+
}
92+
93+
@Test
94+
@EnabledIfSystemProperty(named = "sun.net.http.allowRestrictedHeaders", matches = "true")
95+
public void noRequestBodyForPostWithAllowRestrictedHeaders() throws Exception {
96+
super.noResponseBodyForPost();
97+
MockWebServerAssertions.assertThat(server.takeRequest())
98+
.hasMethod("POST")
99+
.hasNoHeaderNamed("Content-Type")
88100
.hasHeaders(entry("Content-Length", Collections.singletonList("0")));
89101
}
90102

103+
@Test
91104
@Override
92105
public void noResponseBodyForPut() throws Exception {
93106
super.noResponseBodyForPut();
94107
MockWebServerAssertions.assertThat(server.takeRequest())
95108
.hasMethod("PUT")
109+
.hasNoHeaderNamed("Content-Type");
110+
}
111+
112+
@Test
113+
@EnabledIfSystemProperty(named = "sun.net.http.allowRestrictedHeaders", matches = "true")
114+
public void noResponseBodyForPutWithAllowRestrictedHeaders() throws Exception {
115+
super.noResponseBodyForPut();
116+
MockWebServerAssertions.assertThat(server.takeRequest())
117+
.hasMethod("PUT")
118+
.hasNoHeaderNamed("Content-Type")
96119
.hasHeaders(entry("Content-Length", Collections.singletonList("0")));
97120
}
98121

0 commit comments

Comments
 (0)