Avoid default Content-Type on POST/PUT/PATCH with empty body · OpenFeign/feign@f8f1f92 · GitHub
Skip to content

Commit f8f1f92

Browse files
committed
Avoid default Content-Type on POST/PUT/PATCH with empty body
DefaultClient wrote a zero-length byte array to the connection's output stream whenever the HTTP method was not GET, even when the request body was empty. Opening that output stream makes HttpURLConnection create an internal "poster" and, as soon as one exists, the JDK defaults Content-Type to application/x-www-form-urlencoded if none was set - even though no bytes are actually written. This regressed in 12.0 (feign#1778, fixing the Content-Length: 0 case) and silently adds an incorrect Content-Type header, which can cause servers to reject the request with 415 Unsupported Media Type. Skip the output stream entirely for empty/absent bodies and set Content-Length: 0 directly, matching the existing null-body path. Fixes #2068 Signed-off-by: seonwoo_jung <79202163+seonwooj0810@users.noreply.github.com>
1 parent 661b398 commit f8f1f92

3 files changed

Lines changed: 32 additions & 9 deletions

File tree

‎core/src/main/java/feign/DefaultClient.java‎

Lines changed: 10 additions & 9 deletions

‎core/src/test/java/feign/client/AbstractClientTest.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,16 @@ public void noResponseBodyForPut() throws Exception {
229229
api.noPutBody();
230230
}
231231

232+
@Test
233+
void emptyStringBodyForPost() throws Exception {
234+
server.enqueue(new MockResponse());
235+
236+
TestInterface api =
237+
newBuilder().target(TestInterface.class, "http://localhost:" + server.getPort());
238+
239+
api.postEmptyStringBody("");
240+
}
241+
232242
/**
233243
* Some client implementation tests should override this test if the PATCH operation is
234244
* unsupported.
@@ -612,6 +622,9 @@ public interface TestInterface {
612622
@RequestLine("POST")
613623
String noPostBody();
614624

625+
@RequestLine("POST /")
626+
String postEmptyStringBody(String body);
627+
615628
@RequestLine("PUT")
616629
String noPutBody();
617630

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

Lines changed: 9 additions & 0 deletions

0 commit comments

Comments
 (0)