2

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);
Marek Sebera
  • 39,650
  • 37
  • 158
  • 244
Tereno
  • 923
  • 1
  • 13
  • 20

2 Answers2

1

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"));
besc
  • 481
  • 3
  • 8
0

First of all you could try using:

MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();

which is the correct way to initialize it, and then:

addDoc.setEntity(entity.build());

as for the boundary your question is not clear whether you need to read a custom boundary or set it, anyway the custom boundary is set in the following way;

multipartEntity.setBoundary("some-mimetype-boundary-unique-string");
dendini
  • 3,842
  • 9
  • 37
  • 74