MultipartEntityBuilder and Charset
Asked Answered
S

7

25

I upgraded my httpmime package, and now my strings are not sent or received as UTF-8

MultipartEntityBuilder entity = MultipartEntityBuilder.create();
Charset chars = Charset.forName("UTF-8");
entity.setCharset(chars);
entity.addTextBody("some_text", some_text);

HttpPost httppost = new HttpPost(url); 
httppost.setEntity(entity.build());
...and so on..

what am I missing? I used to build a StringBody and set the charset in the stringbody, but that is deprecated now, and it just doesn't seem to work

Saks answered 10/10, 2013 at 9:36 Comment(0)
S
23

Solved it :) it turns out that ContentType is now important, and I was sending text that was plain, and also some text that was JSON,

for the plain text, you can use:

entity.addTextBody("plain_text",plain_text,ContentType.TEXT_PLAIN);

and for JSON:

entity.addTextBody("json_text",json_text,ContentType.APPLICATION_JSON);

that way the charset also works on JSON strings (weird, but now OK)

Saks answered 10/10, 2013 at 11:21 Comment(1)
As far as the document page hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/…, org.apache.http.entity.ContentType) says, StringBody(String text, ContentType contentType, Charset charset) is not deprecated in 4.3.xBurble
B
22
entity.addTextBody("plain_text", plain_text);

will use the default ContentType.TEXT_PLAIN which look like this...

public static final ContentType TEXT_PLAIN = ContentType.create("text/plain", Consts.ISO_8859_1);

while ContentType.APPLICATION_JSON is using UTF-8 as below

public static final ContentType APPLICATION_JSON = ContentType.create("application/json", Consts.UTF_8);

So what i did was create our own ContentType like this..

entity.addTextBody("plain_text", plain_text, ContentType.create("text/plain", MIME.UTF8_CHARSET));
Bonanza answered 9/1, 2014 at 12:34 Comment(2)
Above Solution didn't worked for me, this did (http*-4.3.6.jar). +1Camelot
ContentType.create("text/plain", MIME.UTF8_CHARSET) worked for me. You are my hero.Pricilla
D
10

For those who say the accepted solution did not work for them (it didn't for me either), I solved it a different way, using:

builder.addTextBody(key, שלום, ContentType.TEXT_PLAIN.withCharset("UTF-8"));
Dynast answered 7/10, 2016 at 14:25 Comment(0)
L
2

Please try this for

Utf-8:

entity.addTextBody("your text", stringBuffer.toString(), ContentType.create("text/plain", Charset.forName("UTF-8")));
Lesleelesley answered 12/3, 2015 at 11:33 Comment(0)
C
1

In my case, I setup the contentType first like this

ContentType contentType = ContentType.create(
                        HTTP.PLAIN_TEXT_TYPE, HTTP.UTF_8);

and when adding pairs , specify the content type like this

entityBuilder.addTextBody("title",pic.getTitle(),contentType);

as answered here MultipartEntityBuilder and setCharset for UTF-8 sends empty content

Chassidychassin answered 17/8, 2014 at 21:10 Comment(0)
D
0

Changing the JSON string to BinaryUTF8 also is another way that works fine:

MultipartEntityBuilder meb = MultipartEntityBuilder.create();
meb.setCharset(java.nio.charset.StandardCharsets.UTF_8);
byte[] bytes = StringUtils.getBytesUtf8(some_text);
meb.addBinaryBody("JSON", bytes);
Dam answered 22/3, 2023 at 11:31 Comment(0)
E
0

I solved it in this way, using: ContentType.TEXT_PLAIN.withCharset("UTF-8").

MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
entityBuilder.addTextBody("operation", "postdita");
entityBuilder.addTextBody("path", .getPath(),ContentType.TEXT_PLAIN.withCharset("UTF-8"));
entityBuilder.addTextBody("createrev", "false");
entityBuilder.addBinaryBody("editorData", file);
entityBuilder.addTextBody("content-type", "application/x-www-form-urlencoded");
Except answered 1/12, 2023 at 20:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.