简体   繁体   中英

'Improperly formatted request' error after switching from Commons HttpClient to HttpComponents

I've some code, sending a multipart/form-data request to an API. Using Apache's commons-httpclient 3.1 it works, however switching over to httpclient 4.3.5, I face problems with the API. Below you can find both code samples. Since it has to do with the Salesforce API, I've also posted a question to SFSE , since I'm still not sure if it's a problem on my or their side. However, my question here is: Have I migrated the code to 4.3.5 correctly? If yes, is there anything which changed in httpclient's behavior related to executing multipart/form-data requests?

Code samples follow:

commons-httpclient 3.1

String json = "{ \"body\":{ \"messageSegments\":[ { \"type\":\"Text\", \"text\":\"Here is another receipt.\" } ] }, \"capabilities\":{ \"content\":{ \"title\":\"receipt2\"} } }";

PostMethod filePost = new PostMethod("https://eu3.salesforce.com/services/data/v32.0/chatter/feed-elements/<some_feed_element_id>/capabilities/comments/items");
filePost.addRequestHeader("Authorization", token());

StringPart jsonPart = new StringPart("json", json);
jsonPart.setContentType(ContentType.APPLICATION_JSON.getMimeType());

FilePart filePart = new FilePart("feedElementFileUpload", file);
filePart.setContentType(ContentType.APPLICATION_OCTET_STREAM.getMimeType());

Part[] parts = { jsonPart, filePart };
filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
int response = httpclient.executeMethod(filePost);

Wire / context logs: http://pastebin.com/RCg20Ygn

httpclient 4.3.5

String json = "{ \"body\":{ \"messageSegments\":[ { \"type\":\"Text\", \"text\":\"Here is another receipt.\" } ] }, \"capabilities\":{ \"content\":{ \"title\":\"receipt2\"} } }";

String attachmentName = "package.xml";
CloseableHttpClient client = HttpClientBuilder
    .create()
    .setDefaultHeaders(Lists.newArrayList())
    .build();
HttpPost post = new HttpPost(
   "https://eu3.salesforce.com/services/data/v32.0/chatter/feed-elements/<feed_element_id>/capabilities/comments/items"
);
post.addHeader(HttpHeaders.AUTHORIZATION, token());
post.addHeader(HttpHeaders.CONTENT_TYPE, ContentType.MULTIPART_FORM_DATA.getMimeType());

post.setEntity(
    MultipartEntityBuilder.create()
        .setStrictMode()
        .addPart(
            "json",
            new StringBody(
                json,
                ContentType.APPLICATION_JSON
            )
        )
        .addPart(
            "feedElementFileUpload",
            new FileBody(
                new File(attachmentName),
                ContentType.APPLICATION_OCTET_STREAM,
                attachmentName
            )
        )
        .build()
);
CloseableHttpResponse response = client.execute(post);

Wire / context logs: http://pastebin.com/EHXd1y50

UPDATE 1:

I've tried all three available modes for MultipartEntityBuilder ( STRICT , BROWSER_COMPATIBLE , RFC6532 ), but it still doesn't work.

Try using 'browser compatible' mode instead of 'strict' when constructing the request entity with MultipartEntityBuilder

UPDATE 1:

"Content-Type: multipart/form-data[\r][\n]"

This is clearly wrong (boundary attribute is missing) and likely to be reason for the request being rejected.

Please remove this line and try again

post.addHeader(HttpHeaders.CONTENT_TYPE, ContentType.MULTIPART_FORM_DATA.getMimeType());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM