Skip to content

Commit da53d09

Browse files
committed
Support posting form without charset parameter
Specifying a null or empty charset will not add the charset to the Content-Type header of the request but will encoding the parameters using UTF-8
1 parent 58f00a9 commit da53d09

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

lib/src/main/java/com/github/kevinsawicki/http/HttpRequest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2801,13 +2801,14 @@ public HttpRequest form(final Object name, final Object value)
28012801
* @return this request
28022802
* @throws HttpRequestException
28032803
*/
2804-
public HttpRequest form(final Object name, final Object value,
2805-
final String charset) throws HttpRequestException {
2804+
public HttpRequest form(final Object name, final Object value, String charset)
2805+
throws HttpRequestException {
28062806
final boolean first = !form;
28072807
if (first) {
28082808
contentType(CONTENT_TYPE_FORM, charset);
28092809
form = true;
28102810
}
2811+
charset = getValidCharset(charset);
28112812
try {
28122813
openOutput();
28132814
if (!first)

lib/src/test/java/com/github/kevinsawicki/http/HttpRequestTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,11 +729,13 @@ public void handle(Request request, HttpServletResponse response) {
729729
@Test
730730
public void postForm() throws Exception {
731731
final AtomicReference<String> body = new AtomicReference<String>();
732+
final AtomicReference<String> contentType = new AtomicReference<String>();
732733
handler = new RequestHandler() {
733734

734735
@Override
735736
public void handle(Request request, HttpServletResponse response) {
736737
body.set(new String(read()));
738+
contentType.set(request.getContentType());
737739
response.setStatus(HTTP_OK);
738740
}
739741
};
@@ -743,6 +745,35 @@ public void handle(Request request, HttpServletResponse response) {
743745
int code = post(url).form(data).form("zip", "12345").code();
744746
assertEquals(HTTP_OK, code);
745747
assertEquals("name=user&number=100&zip=12345", body.get());
748+
assertEquals("application/x-www-form-urlencoded; charset=UTF-8",
749+
contentType.get());
750+
}
751+
752+
/**
753+
* Make a post of form data
754+
*
755+
* @throws Exception
756+
*/
757+
@Test
758+
public void postFormWithNoCharset() throws Exception {
759+
final AtomicReference<String> body = new AtomicReference<String>();
760+
final AtomicReference<String> contentType = new AtomicReference<String>();
761+
handler = new RequestHandler() {
762+
763+
@Override
764+
public void handle(Request request, HttpServletResponse response) {
765+
body.set(new String(read()));
766+
contentType.set(request.getContentType());
767+
response.setStatus(HTTP_OK);
768+
}
769+
};
770+
Map<String, String> data = new LinkedHashMap<String, String>();
771+
data.put("name", "user");
772+
data.put("number", "100");
773+
int code = post(url).form(data, null).form("zip", "12345").code();
774+
assertEquals(HTTP_OK, code);
775+
assertEquals("name=user&number=100&zip=12345", body.get());
776+
assertEquals("application/x-www-form-urlencoded", contentType.get());
746777
}
747778

748779
/**

0 commit comments

Comments
 (0)