diff --git a/core/src/test/java/feign/client/AbstractClientTest.java b/core/src/test/java/feign/client/AbstractClientTest.java index a2421a3461..704d88b425 100644 --- a/core/src/test/java/feign/client/AbstractClientTest.java +++ b/core/src/test/java/feign/client/AbstractClientTest.java @@ -15,6 +15,7 @@ import feign.Param; import feign.RequestLine; import feign.Response; +import feign.Util; import feign.assertj.MockWebServerAssertions; import okhttp3.mockwebserver.MockResponse; import okhttp3.mockwebserver.MockWebServer; @@ -218,6 +219,30 @@ public void testResponseLength() throws Exception { assertEquals(expected, actual); } + @Test + public void testContentTypeWithCharset() throws Exception { + server.enqueue(new MockResponse() + .setBody("AAAAAAAA")); + TestInterface api = newBuilder() + .target(TestInterface.class, "http://localhost:" + server.getPort()); + + Response response = api.postWithContentType("foo", "text/plain;charset=utf-8"); + // Response length should not be null + assertEquals("AAAAAAAA", Util.toString(response.body().asReader())); + } + + @Test + public void testContentTypeWithoutCharset() throws Exception { + server.enqueue(new MockResponse() + .setBody("AAAAAAAA")); + TestInterface api = newBuilder() + .target(TestInterface.class, "http://localhost:" + server.getPort()); + + Response response = api.postWithContentType("foo", "text/plain"); + // Response length should not be null + assertEquals("AAAAAAAA", Util.toString(response.body().asReader())); + } + public interface TestInterface { @RequestLine("POST /?foo=bar&foo=baz&qux=") @@ -241,6 +266,10 @@ public interface TestInterface { @RequestLine("PUT") String noPutBody(); + + @RequestLine("POST /?foo=bar&foo=baz&qux=") + @Headers({"Foo: Bar", "Foo: Baz", "Qux: ", "Content-Type: {contentType}"}) + Response postWithContentType(String body, @Param("contentType") String contentType); } } diff --git a/httpclient/src/main/java/feign/httpclient/ApacheHttpClient.java b/httpclient/src/main/java/feign/httpclient/ApacheHttpClient.java index 8cfebf0a63..18fbc7512e 100644 --- a/httpclient/src/main/java/feign/httpclient/ApacheHttpClient.java +++ b/httpclient/src/main/java/feign/httpclient/ApacheHttpClient.java @@ -153,13 +153,13 @@ HttpUriRequest toHttpUriRequest(Request request, Request.Options options) throws private ContentType getContentType(Request request) { ContentType contentType = ContentType.DEFAULT_TEXT; for (Map.Entry> entry : request.headers().entrySet()) - if (entry.getKey().equalsIgnoreCase("Content-Type")) { - Collection values = entry.getValue(); - if (values != null && !values.isEmpty()) { - contentType = ContentType.create(entry.getValue().iterator().next(), request.charset()); - break; + if (entry.getKey().equalsIgnoreCase("Content-Type")) { + Collection values = entry.getValue(); + if (values != null && !values.isEmpty()) { + contentType = ContentType.parse(values.iterator().next()); + break; + } } - } return contentType; }