Java HttpClient 3.1 Multipart Post

I am using Java HttpClient 3.1 to encode a REST API call. I ran into a problem for Post with multipart/form-data. Here's the API call I'm trying to make, with an example provided by the API provider:

POST /v1/documents HTTP/1.1
Host: .......
x-session-key: 02e57c7d-d071-4c63-b491-1194a9939ea5.2016-01-13T22:34:53.101Z
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="documentImage"; filename="ScreenShot 2016-01-14 at 1.48.48 PM.png"
Content-Type: image/png


----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="documentType"

Picture ID
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="token"

02e57c7d-d071-4c63-b491-1194a9939ea5
----WebKitFormBoundary7MA4YWxkTrZu0gW

Here is my code with HttpClient 3.1:

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;


Part[] parts = new Part[3];
parts[0] = new StringPart("token", this.user.userID);
parts[1] = new StringPart("documentType", "Picture ID");

try {
    File imageFile = new File("/path/test2.png");
    parts[2] = new FilePart("documentImage", imageFile);
} catch (FileNotFoundException ex) {
    .....
}

PostMethod postMethod = new PostMethod(hostURL + "/documents");

MultipartRequestEntity entity = new MultipartRequestEntity(parts, postMethod.getParams());
postMethod.setRequestEntity(entity);

postMethod.addRequestHeader("Content-Type", "multipart/form-data");
postMethod.addRequestHeader("x-session-key", "sjkkjksjkkjdjk");

HttpClient httpClient = new HttpClient();
int statusCode = 0;
try {
    statusCode = httpClient.executeMethod(httpMethod);
} catch (IOException ex) {
    ....
}

String respond = null;
try {
    StringBuilder resultStr = new StringBuilder();
    try (BufferedReader rd = new BufferedReader(new InputStreamReader(httpMethod.getResponseBodyAsStream()))) {
        String line;
        while ((line = rd.readLine()) != null) {
            resultStr.append(line);
        }
    }
    respond = resultStr.toString();
    httpMethod.releaseConnection();
} catch (IOException ex) {
    .....
}

System.out.println("status code: " + statusCode + ", respond: " + respond);

Answer what I get from the API server:

status code: 400, respond: {"code":400,"message":"Unknown content type [contentType=application/octet-stream]"}

I have already installed multipart/form-datausingpostMethod.addRequestHeader("Content-Type", "multipart/form-data");

Did I miss something?

+4
source share
1 answer

I understood the solution. Here's the fix:

parts[2] = new FilePart("documentImage", f, "image/png", null);

What he does is install CONTENT_TYPEfor FilePart. DEFAULT_CONTENT_TYPEis application/octet-streamif not specified in the constructor.

The constructor method that I use:

public FilePart(String name,
            PartSource partSource,
            String contentType,
            String charset)
+1

All Articles