Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache HttpClient making multipart form post

I'm pretty green to HttpClient and I'm finding the lack of (and or blatantly incorrect) documentation extremely frustrating. I'm trying to implement the following post (listed below) with Apache Http Client, but have no idea how to actually do it. I'm going to bury myself in documentation for the next week, but perhaps more experienced HttpClient coders could get me an answer sooner.

Post:

Content-Type: multipart/form-data; boundary=---------------------------1294919323195 Content-Length: 502 -----------------------------1294919323195 Content-Disposition: form-data; name="number"  5555555555 -----------------------------1294919323195 Content-Disposition: form-data; name="clip"  rickroll -----------------------------1294919323195 Content-Disposition: form-data; name="upload_file"; filename="" Content-Type: application/octet-stream   -----------------------------1294919323195 Content-Disposition: form-data; name="tos"  agree -----------------------------1294919323195-- 
like image 599
Russ Avatar asked Feb 21 '10 03:02

Russ


2 Answers

Use MultipartEntityBuilder from the HttpMime library to perform the request you want.

In my project I do that this way:

HttpEntity entity = MultipartEntityBuilder     .create()     .addTextBody("number", "5555555555")     .addTextBody("clip", "rickroll")     .addBinaryBody("upload_file", new File(filePath), ContentType.APPLICATION_OCTET_STREAM, "filename")     .addTextBody("tos", "agree")     .build();  HttpPost httpPost = new HttpPost("http://some-web-site"); httpPost.setEntity(entity); HttpResponse response = httpClient.execute(httpPost); HttpEntity result = response.getEntity(); 

Hope this will help.

(Updated this post to use MultipartEntityBuilder instead of deprecated MultipartEntity, using @mtomy code as the example)

like image 84
mtomy Avatar answered Oct 13 '22 06:10

mtomy


MultipartEntity now shows up as deprecated. I am using apache httpclient 4.3.3 - does anyone know what we are supposed to use instead? I find the google searches to be so full of MultipartEntity examples I can't find anything. – vextorspace Mar 31 '14 at 20:36

Here is the sample code in HttpClient 4.3.x

http://hc.apache.org/httpcomponents-client-4.3.x/httpmime/examples/org/apache/http/examples/entity/mime/ClientMultipartFormPost.java

import org.apache.http.entity.mime.MultipartEntityBuilder;  HttpPost httppost = new HttpPost("http://localhost:8080" +         "/servlets-examples/servlet/RequestInfoExample");  FileBody bin = new FileBody(new File(args[0])); StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN);  HttpEntity reqEntity = MultipartEntityBuilder.create()         .addPart("bin", bin)         .addPart("comment", comment)         .build();   httppost.setEntity(reqEntity); 

To use the class MultipartEntityBuilder, you need httpmime, which is a sub project of HttpClient

HttpClient 4.3.x:

http://hc.apache.org/httpcomponents-client-4.3.x/index.html

httpmime 4.3.x:

http://hc.apache.org/httpcomponents-client-4.3.x/httpmime/dependency-info.html

like image 29
Li Ying Avatar answered Oct 13 '22 05:10

Li Ying