Skip to content

Commit 8a5ada3

Browse files
committed
Merge pull request AsyncHttpClient#540 from kelvl/content-type-body-charset-sync
Propagate charset from Content-Type into Request's bodyEncoding
2 parents 8a7328c + d6b992a commit 8a5ada3

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/main/java/com/ning/http/client/RequestBuilderBase.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,9 +612,22 @@ public T setConnectionPoolKeyStrategy(ConnectionPoolKeyStrategy connectionPoolKe
612612
}
613613

614614
public Request build() {
615+
try {
616+
final String contentType = request.headers.getFirstValue("Content-Type");
617+
if (contentType != null) {
618+
final String charset = AsyncHttpProviderUtils.parseCharset(contentType);
619+
if (charset != null) {
620+
// ensure that if charset is provided with the Content-Type header,
621+
// we propagate that down to the charset of the Request object
622+
request.charset = charset;
623+
}
624+
}
625+
} catch (Throwable e) {
626+
// NoOp -- we can't fix the Content-Type or charset from here
627+
}
615628
if (request.length < 0 && request.streamData == null) {
616629
// can't concatenate content-length
617-
String contentLength = request.headers.getFirstValue("Content-Length");
630+
final String contentLength = request.headers.getFirstValue("Content-Length");
618631

619632
if (contentLength != null) {
620633
try {

src/test/java/com/ning/http/client/async/RequestBuilderTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,12 @@ public void testUserProvidedRequestMethod() {
105105
assertEquals(req.getMethod(), "ABC");
106106
assertEquals(req.getUrl(), "http://foo.com");
107107
}
108+
109+
@Test(groups = {"standalone", "default_provider"})
110+
public void testContentTypeCharsetToBodyEncoding() {
111+
final Request req = new RequestBuilder("GET").setHeader("Content-Type", "application/json; charset=utf-8").build();
112+
assertEquals(req.getBodyEncoding(), "utf-8");
113+
final Request req2 = new RequestBuilder("GET").setHeader("Content-Type", "application/json; charset=\"utf-8\"").build();
114+
assertEquals(req2.getBodyEncoding(), "utf-8");
115+
}
108116
}

0 commit comments

Comments
 (0)