You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Arya F <ar...@gmail.com> on 2018/02/27 00:27:00 UTC

Apache HttpClient include boundary in multipart/form-data

I made a post about this on Stackoverflow and never received an answer.
Here is the link to the post
https://stackoverflow.com/questions/48994951/apache-httpclient-include-boundary-in-multipart-form-data

Here is what I am stuck on

I have the following POST request which is supposed to upload a file. But I
can not figure out how to include the boundary in the request for the
"Content-Type" header.


HttpPost request = new HttpPost(url);
request.setConfig(config);

StringEntity params = new StringEntity("");

HttpEntity entity = MultipartEntityBuilder.create()
        .addBinaryBody("blob", file,
ContentType.create("application/octet-stream"), "filename").build();

request.addHeader("Host", "upload.twitter.com");
request.addHeader("Connection", "keep-alive");
request.addHeader("User-Agent", userAgent);
request.addHeader("Content-Type",  ????????);
request.addHeader("Accept", "*/*");
request.addHeader("Accept-Encoding", "gzip, deflate, br");
request.addHeader("Accept-Language", "en-US,en;q=0.9");
request.addHeader("Cookie", cookies);

request.setEntity(entity);
response = httpClient.execute(request);

int responseCode = response.getStatusLine().getStatusCode();

System.out.println("upload response code: " + responseCode);



any idea how this is done?

Re: Apache HttpClient include boundary in multipart/form-data

Posted by Bindul Bhowmik <bi...@gmail.com>.
On Mon, Feb 26, 2018 at 5:27 PM, Arya F <ar...@gmail.com> wrote:
> I made a post about this on Stackoverflow and never received an answer.
> Here is the link to the post
> https://stackoverflow.com/questions/48994951/apache-httpclient-include-boundary-in-multipart-form-data
>
> Here is what I am stuck on
>
> I have the following POST request which is supposed to upload a file. But I
> can not figure out how to include the boundary in the request for the
> "Content-Type" header.
>
>
> HttpPost request = new HttpPost(url);
> request.setConfig(config);
>
> StringEntity params = new StringEntity("");
>
> HttpEntity entity = MultipartEntityBuilder.create()
>         .addBinaryBody("blob", file,
> ContentType.create("application/octet-stream"), "filename").build();
>
> request.addHeader("Host", "upload.twitter.com");
> request.addHeader("Connection", "keep-alive");
> request.addHeader("User-Agent", userAgent);
> request.addHeader("Content-Type",  ????????);
> request.addHeader("Accept", "*/*");
> request.addHeader("Accept-Encoding", "gzip, deflate, br");
> request.addHeader("Accept-Language", "en-US,en;q=0.9");
> request.addHeader("Cookie", cookies);
>
> request.setEntity(entity);
> response = httpClient.execute(request);

Arya,

I am not sure why you are adding all the request headers manually.
Things like the Content-Type should be handled from the entity
automatically for you.

A simple example code is available on the site at
https://hc.apache.org/httpcomponents-client-ga/httpmime/examples/org/apache/http/examples/entity/mime/ClientMultipartFormPost.java

Bindul

>
> int responseCode = response.getStatusLine().getStatusCode();
>
> System.out.println("upload response code: " + responseCode);
>
>
>
> any idea how this is done?

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org