Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java HttpClient 3.1 Multipart Post

I am using Java HttpClient 3.1 to code for REST API call. I run into problem for Post with multipart/form-data. Here's the API call I am trying to make, with example given by 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's 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);

Respond that I get from API server is:

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

I already set multipart/form-data with postMethod.addRequestHeader("Content-Type", "multipart/form-data");

Am I missing something?

like image 894
Shuwn Yuan Tee Avatar asked Oct 19 '22 10:10

Shuwn Yuan Tee


1 Answers

I figured out the solution. Here's the fix:

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

What it does is setting CONTENT_TYPE for FilePart. DEFAULT_CONTENT_TYPE is application/octet-stream if not set in constructor.

Constructor method I use is:

public FilePart(String name,
            PartSource partSource,
            String contentType,
            String charset)
like image 157
Shuwn Yuan Tee Avatar answered Oct 21 '22 05:10

Shuwn Yuan Tee