Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Content-Type Header when request with empty body POST method #2555

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,12 @@ in a context-specific manner -- for example, thread-local storage can be used to
header values depending on the invoking thread, which can be useful for things such as setting
thread-specific trace identifiers for requests.

#### Set zero Content-Length Header

To specify `Content-Length: 0` header when making a request with empty body, system property `sun.net.http.allowRestrictedHeaders` should be set to `true`

If not, the `Content-Length` header will not be added.

### Advanced usage

#### Base Apis
Expand Down
13 changes: 7 additions & 6 deletions core/src/main/java/feign/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,19 +204,14 @@ else if (field.equals(ACCEPT_ENCODING)) {
connection.addRequestProperty("Accept", "*/*");
}

boolean hasEmptyBody = false;
byte[] body = request.body();
if (body == null && request.httpMethod().isWithBody()) {
body = new byte[0];
hasEmptyBody = true;
}

if (body != null) {
/*
* Ignore disableRequestBuffering flag if the empty body was set, to ensure that internal
* retry logic applies to such requests.
*/
if (disableRequestBuffering && !hasEmptyBody) {
if (disableRequestBuffering) {
if (contentLength != null) {
connection.setFixedLengthStreamingMode(contentLength);
} else {
Expand All @@ -239,6 +234,12 @@ else if (field.equals(ACCEPT_ENCODING)) {
}
}
}

if (body == null && request.httpMethod().isWithBody()) {
// To use this Header, set 'sun.net.http.allowRestrictedHeaders' property true.
connection.addRequestProperty("Content-Length", "0");
}

return connection;
}

Expand Down
23 changes: 23 additions & 0 deletions core/src/test/java/feign/client/DefaultClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.SocketPolicy;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;

/** Tests client-specific behavior, such as ensuring Content-Length is sent when specified. */
public class DefaultClientTest extends AbstractClientTest {
Expand Down Expand Up @@ -78,19 +79,41 @@ public void patch() throws Exception {
assertThat(exception).hasCauseInstanceOf(ProtocolException.class);
}

@Test
@Override
public void noResponseBodyForPost() throws Exception {
super.noResponseBodyForPost();
MockWebServerAssertions.assertThat(server.takeRequest())
.hasMethod("POST")
.hasNoHeaderNamed("Content-Type");
}

@Test
@EnabledIfSystemProperty(named = "sun.net.http.allowRestrictedHeaders", matches = "true")
public void noRequestBodyForPostWithAllowRestrictedHeaders() throws Exception {
super.noResponseBodyForPost();
MockWebServerAssertions.assertThat(server.takeRequest())
.hasMethod("POST")
.hasNoHeaderNamed("Content-Type")
.hasHeaders(entry("Content-Length", Collections.singletonList("0")));
}

@Test
@Override
public void noResponseBodyForPut() throws Exception {
super.noResponseBodyForPut();
MockWebServerAssertions.assertThat(server.takeRequest())
.hasMethod("PUT")
.hasNoHeaderNamed("Content-Type");
}

@Test
@EnabledIfSystemProperty(named = "sun.net.http.allowRestrictedHeaders", matches = "true")
public void noResponseBodyForPutWithAllowRestrictedHeaders() throws Exception {
super.noResponseBodyForPut();
MockWebServerAssertions.assertThat(server.takeRequest())
.hasMethod("PUT")
.hasNoHeaderNamed("Content-Type")
.hasHeaders(entry("Content-Length", Collections.singletonList("0")));
}

Expand Down
Loading