Skip to content
Open
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
4 changes: 3 additions & 1 deletion core/src/main/java/feign/DefaultClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ else if (field.equals(ACCEPT_ENCODING)) {
}
}

if (body == null && request.httpMethod().isWithBody()) {
if (body == null
&& request.httpMethod().isWithBody()
&& !request.headers().containsKey(CONTENT_LENGTH)) {
// To use this Header, set 'sun.net.http.allowRestrictedHeaders' property true.
connection.addRequestProperty("Content-Length", "0");
}
Expand Down
21 changes: 21 additions & 0 deletions core/src/test/java/feign/client/DefaultClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import feign.DefaultClient;
import feign.Feign;
import feign.Feign.Builder;
import feign.Headers;
import feign.RequestLine;
import feign.RetryableException;
import feign.assertj.MockWebServerAssertions;
import java.io.IOException;
Expand Down Expand Up @@ -101,6 +103,25 @@ public void noRequestBodyForPostWithAllowRestrictedHeaders() throws Exception {
.hasHeaders(entry("Content-Length", Collections.singletonList("0")));
}

@Test
@EnabledIfSystemProperty(named = "sun.net.http.allowRestrictedHeaders", matches = "true")
public void contentLengthHeaderIsNotDuplicatedForBodylessRequest() throws Exception {
server.enqueue(new MockResponse());

ContentLengthInterface api =
newBuilder().target(ContentLengthInterface.class, "http://localhost:" + server.getPort());

api.postEmpty();

assertThat(server.takeRequest().getHeaders().values("Content-Length")).containsExactly("0");
}

interface ContentLengthInterface {
@RequestLine("POST /")
@Headers("Content-Length: 0")
void postEmpty();
}

@Test
@Override
public void noResponseBodyForPut() throws Exception {
Expand Down