I am using Apache HttpComponents v4.3.3 (maven httpclient and httpmime). I need to upload a file with some metadata. The curl command, which works, looks like the following.
curl -k -i -H "Content-Type: multipart/mixed" -X POST --form 'field1=val1' --form 'field2=val2' --form 'file=@somefile.zip;type=application/zip' https://www.some.domain/
I have tried mimicking this curl post as the following.
HttpEntity entity = MultiPartEntityBuilder
.create()
.addPart("field1",new StringBody("val1",ContentType.TEXT_PLAIN))
.addPart("field2",new StringBody("val2",ContentType.TEXT_PLAIN))
.addPart("file", new FileBody(new File("somefile.zip"), ContentType.create("application/zip"))
.build();
HttpPost post = new HttpPost("https://www.some.domain");
post.addHeader("Content-Type", "multipart/mixed");
However, after I use HttpClient to execute the HttpPost, I get the following exception (server code is also Java running on Jetty).
org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
When I add a trace to curl
curl --trace - -k -i -H "Content-Type: multipart/mixed" -X POST --form 'field1=val1' --form 'field2=val2' --form 'file=@somefile.zip;type=application/zip' https://www.some.domain/
I see that the form field/value pairs are set as HTTP headers.
Content-Disposition: form-data; name=field1...value1
Any idea on what I'm doing wrong here? Any help is appreciated.