Discussion:
Apache HttpClient include boundary in multipart/form-data
Arya F
2018-02-27 00:27:00 UTC
Permalink
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?
Bindul Bhowmik
2018-02-27 01:44:46 UTC
Permalink
Post by Arya F
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
Post by Arya F
int responseCode = response.getStatusLine().getStatusCode();
System.out.println("upload response code: " + responseCode);
any idea how this is done?
---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-***@hc.apache.org
For additional commands, e-mail: httpclient-users-***@hc.apache.org
Loading...