java - how do i create and post a multipart/mixed http request using Apache HttpComponentst? -
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 / composite" -x POST --form 'field1 = val1' - --form 'field2 = val2' --form ' file =@somefile.zip; Type = application / zip '
I have tried copying this curl post as the following.
HttpEntity unit = MultiPartEntityBuilder .create () .addPart ("Field 1", New StringBody ("VAL1", ContentType.TEXT_PLAIN)) .addPart ("field2", New StringBody ("val2 ".htpPost post = new HttpPost (" File "," File ", New FileBody (New file (" somefile.zip "), ContentType.create (" application / jip ")) .build () Https: // www ("content-type", "multipart / mixed");
However, when I use HTTPL to execute the HTTP post, Exceptions (Server code is also running on JET).
org.apache.commons.fileupload.FileUploadException: The request was rejected because no multipath limit was found
When I add a trace to the curl
curl --trace - -k -i-h "content-type: multipart / composite" -ex post --form 'field1 = val1' --form 'field2 = val2' --form 'file =@somefile.zip; type = application / zip'
I see that form field / value pairs are set as HTTP headers goes.
Content-Vision: Form-Data; Name = field1 ... value1
Any ideas on what I am doing wrong here? Any help is appreciated.
I did a little bit tinkered and two things to do code work.
- Add Now (...)
- No content-type header set now
Here is the modified snippet in which case work Anyone is interested in doing so. .addTextBody ("field2", "val2") .addBinaryBody ("file", new file). ("Somefile.zip"), create content type ("app / zip"), "somefile.zip") .build (); HTTP Post Post = New HTTP Post ("https: //www.some.domain"); Post.setEntity (unit);
I have also set the HttpComponents to debug mode.
-Dorg.apache.commons.logging.Log = org.apache.commons.logging.impl.SimpleLog-Dorg.apache.commons.logging.simplelog.showdatetime = true -Dorg.apache.commons. Logging.simplelog.log.org.apache.http = DEBUGIt has been found that each part has a limit now. Even better yet, the content type and range are autogenerated.
Content type: multipart / form-data; Range = 5ejxpaJqXwk2n_3IVZagQ1U0_J_X9MdGvst9n2Tc
Comments
Post a Comment