Question

I am calling a web service which expects some string data as multipart form input.

POST /add_or_update HTTP/1.1
 User-Agent: Apache-HttpClient/4.3.3 (java 1.5)
 Host: localhost:8011
 Accept: */*
 token:728-e087-ca01-08d34
 Content-Length: 691
 Expect: 100-continue
 Content-Type: multipart/form-data; charset=ISO-8859-1

"--Kd2aTBM_weD045iHeEcrWPLhENlKkH_L6oBz"

"Content-Disposition : form-data; name="abc" "first""

"--Kd2aTBM_weD045iHeEcrWPLhENlKkH_L6oBz"

"Content-Disposition : form-data; name="type" "AD""

If I am using Apache HTTP client, the underlying service seems to throw an error.

Invalid boundary in multipart form ''

If I make this same request via CURL, it seems to work as expected. Here are the headers for it.

POST /add_or_update HTTP/1.1
User-Agent: curl/7.27.0
Host: localhost:8011
Accept: */*
token:728-001-ca01-08d34
Content-Length: 691
Expect: 100-continue
Content-Type: multipart/form-data; boundary=----------------------------626b33b5dc95

The only difference I notice is that Apache HTTP client (4.2) does not use the boundary marker in the header but it still specifies the boundary in the payload, and I am guessing, that is causing the request to fail.

Has anyone been in a similar situation? Any pointers would be really appreciated. Thanks in advance.

Was it helpful?

Solution

If you're manually setting the Content Type with HTTP Client, you are probably wiping out the Content Type HTTP Client sets with the boundary string.

You should only be setting the content type for each part and not the entire HTTP Request. See https://hc.apache.org/httpcomponents-client-4.3.x/httpmime/apidocs/org/apache/http/entity/mime/MultipartEntityBuilder.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top