Send Unicode characters via MultipartEntity
I solved it a different way, using:
builder.addTextBody(key, שלום, ContentType.TEXT_PLAIN.withCharset("UTF-8"));
You can use below line for add part in multipart entity
entity.addPart("Data", new StringBody(data,Charset.forName("UTF-8")));
to send unicode in request.
To the ones who stuck with this issue, this is how I resolved it:
I investigated apache http components libraries source code and found following:
org.apache.http.entity.mime.HttpMultipart::doWriteTo()case BROWSER_COMPATIBLE: // Only write Content-Disposition // Use content charset final MinimalField cd = part.getHeader().getField(MIME.CONTENT_DISPOSITION); writeField(cd, this.charset, out); final String filename = part.getBody().getFilename(); if (filename != null) { final MinimalField ct = part.getHeader().getField(MIME.CONTENT_TYPE); writeField(ct, this.charset, out); } break;
So, seems like it is some kind of bug / feature in apache lib, which only allowes to add Content-type header to one part of MultipartEntity, if this part has filename not null. So I modified my code as:
Charset utf8 = Charset.forName("utf-8");ContentType contentType = ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), utf8);ContentBody body = new ByteArrayBody(mMessage.getBytes(), contentType, "filename");mpEntity.addPart("message", body);
And Content-type header appeared for string part, and symbols are now encoded and decoded correctly.