appsly-android-rest icon indicating copy to clipboard operation
appsly-android-rest copied to clipboard

how to do post formdata with utf-8?

Open haemi opened this issue 9 years ago • 4 comments

I'm using Umlaute (ä, ö, ü) in my post parameters, which aren't submitted correctly, therefore I'm looking for a way to set the encoding to UTF-8; is that possible?

haemi avatar Sep 15 '14 05:09 haemi

update: it seems that the encoding is set to latin-1, as e.g. ß is sent as %DF

haemi avatar Sep 17 '14 15:09 haemi

I want the request header to be

Content-Type: application /x-www-form-urlencoded ;charset=utf-8

I but got

Content-Type: application /x-www-form-urlencoded 

I've tried@FormField(contentType = "application /x-www-form-urlencoded ;charset=utf-8") but it didn't work.

how can I modify the request header?

yhd4711499 avatar Nov 06 '14 14:11 yhd4711499

update: I've found a solution.

User a customized request body converter

here is the code

client = (DefaultRestClientImpl)RestClientFactory.defaultClient(ctx);

        client.setConverter(new DelegatingConverterService(){
            {
                addConverter(new JacksonBodyConverter());
                addConverter(new JacksonHttpFormUtf8ValuesConverter());
            }});

JacksonHttpFormUtf8ValuesConverter is a copy from original JacksonHttpFormValuesConverter with a modification in public <T> HttpEntity toRequestBody(T object, String contentType) method.

// line 102
entity =  new UrlEncodedFormEntity(vals, "UTF-8");

I'm still finding solutions for HeaderUtils.CONTENT_TYPE_MULTIPART_FORM_DATA condition.

yhd4711499 avatar Nov 07 '14 02:11 yhd4711499

@haemi Yours is a valid use case. For the HeaderUtils.CONTENT_TYPE_MULTIPART_FORM_DATA the lib would need to change to not match exactly on the header value but the beginning of it so the charset is just passed through. We made the decision back then to not do that because From Data and Query sting params are usually url encoded. You can decode them in your server with something like this: http://stackoverflow.com/questions/546365/utf-8-text-is-garbled-when-form-is-posted-as-multipart-form-data

Let me know if that helps and if not we can look into a PR that laxes the requirements when matching the header names so you can send UTF-8 encoded stuff. Thanks!

raulraja avatar Nov 07 '14 17:11 raulraja