Skip to content

Commit 8fd94ce

Browse files
stolyarchukavadriancole
authored andcommitted
Support request content-type with charset (OpenFeign#453)
1 parent 3adb897 commit 8fd94ce

2 files changed

Lines changed: 35 additions & 6 deletions

File tree

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import feign.Param;
1616
import feign.RequestLine;
1717
import feign.Response;
18+
import feign.Util;
1819
import feign.assertj.MockWebServerAssertions;
1920
import okhttp3.mockwebserver.MockResponse;
2021
import okhttp3.mockwebserver.MockWebServer;
@@ -218,6 +219,30 @@ public void testResponseLength() throws Exception {
218219
assertEquals(expected, actual);
219220
}
220221

222+
@Test
223+
public void testContentTypeWithCharset() throws Exception {
224+
server.enqueue(new MockResponse()
225+
.setBody("AAAAAAAA"));
226+
TestInterface api = newBuilder()
227+
.target(TestInterface.class, "http://localhost:" + server.getPort());
228+
229+
Response response = api.postWithContentType("foo", "text/plain;charset=utf-8");
230+
// Response length should not be null
231+
assertEquals("AAAAAAAA", Util.toString(response.body().asReader()));
232+
}
233+
234+
@Test
235+
public void testContentTypeWithoutCharset() throws Exception {
236+
server.enqueue(new MockResponse()
237+
.setBody("AAAAAAAA"));
238+
TestInterface api = newBuilder()
239+
.target(TestInterface.class, "http://localhost:" + server.getPort());
240+
241+
Response response = api.postWithContentType("foo", "text/plain");
242+
// Response length should not be null
243+
assertEquals("AAAAAAAA", Util.toString(response.body().asReader()));
244+
}
245+
221246
public interface TestInterface {
222247

223248
@RequestLine("POST /?foo=bar&foo=baz&qux=")
@@ -241,6 +266,10 @@ public interface TestInterface {
241266

242267
@RequestLine("PUT")
243268
String noPutBody();
269+
270+
@RequestLine("POST /?foo=bar&foo=baz&qux=")
271+
@Headers({"Foo: Bar", "Foo: Baz", "Qux: ", "Content-Type: {contentType}"})
272+
Response postWithContentType(String body, @Param("contentType") String contentType);
244273
}
245274

246275
}

httpclient/src/main/java/feign/httpclient/ApacheHttpClient.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,13 @@ HttpUriRequest toHttpUriRequest(Request request, Request.Options options) throws
153153
private ContentType getContentType(Request request) {
154154
ContentType contentType = ContentType.DEFAULT_TEXT;
155155
for (Map.Entry<String, Collection<String>> entry : request.headers().entrySet())
156-
if (entry.getKey().equalsIgnoreCase("Content-Type")) {
157-
Collection values = entry.getValue();
158-
if (values != null && !values.isEmpty()) {
159-
contentType = ContentType.create(entry.getValue().iterator().next(), request.charset());
160-
break;
156+
if (entry.getKey().equalsIgnoreCase("Content-Type")) {
157+
Collection<String> values = entry.getValue();
158+
if (values != null && !values.isEmpty()) {
159+
contentType = ContentType.parse(values.iterator().next());
160+
break;
161+
}
161162
}
162-
}
163163
return contentType;
164164
}
165165

0 commit comments

Comments
 (0)