Making an HttpRequest with MultipartEntity in it

Tereno picture Tereno · Jun 30, 2010 · Viewed 7.9k times · Source

I've been frustrated at trying to figure out how to make an http request with a multipart entity in it. The multipart has a custom boundary but I can't seem to be able to set it. My code below results in a response message of saying that my message does not contain multiple parts.

HttpPut addDoc = new HttpPut(url);
addDoc.addHeader("Content-Type", "multipart/related; boundary=\"END_OF_PART\"");
String bodyString = "Test for multipart update";
String titleString = "Title Test for multipart update";
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
StringBody title = new StringBody(titleString, "application/atom+xml",Charset.forName("UTF-8"));
StringBody body = new StringBody(bodyString, "text/plain",Charset.forName("UTF-8"));
entity.addPart("title", title);
entity.addPart("body", body);
addDoc.setEntity(entity);

Answer

besc picture besc · Apr 11, 2011

you can try to remove addHeader part from your code and change your constructor to

MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE,null,Charset.forName("UTF-8"));