简体   繁体   中英

Bad Request response - Apache HttpClient

I have extracted my working Postman POST request to JSON. I'm trying to configure Apache HttpClient in the same way, but I tried multiple ways and I'm constantly getting Bad Request response.

Could you please advise what is wrong with my HttpClient configuration?

Extract from Postman:

 { "info": { "_postman_id": "eae7c603-8b49-496f-9f71-a16b475f34ce", "name": "Upload Documents", "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json", "_exporter_id": "14164917" }, "item": [ { "name": "Upload a supporting document", "request": { "method": "POST", "header": [], "body": { "mode": "formdata", "formdata": [ { "key": "Key", "value": "someValue", "type": "text" }, { "key": "X-Amz-Algorithm", "value": "AWS4-HMAC-SHA256", "type": "text" }, { "key": "X-Amz-Credential", "value": "someValue", "type": "text" }, { "key": "X-Amz-Date", "value": "20220802T142430Z", "type": "text" }, { "key": "X-Amz-Security-Token", "value": "someValue", "type": "text" }, { "key": "Policy", "value": "someValue", "type": "text" }, { "key": "X-Amz-Signature", "value": "someValue", "type": "text" }, { "key": "X-Amz-Meta-X-Swan-Supporting-Document-Id", "value": "someValue", "type": "text" }, { "key": "file", "type": "file", "src": "/path/test.pdf" } ] }, "url": { "raw": "https://s3.eu-west-1.amazonaws.com/document-sandbox", "protocol": "https", "host": [ "s3", "eu-west-1", "amazonaws", "com" ], "path": [ "document-sandbox" ] } }, "response": [] } ] } 

My code:

private static CloseableHttpResponse upload(String url, Map<String, String> requestParameters, File file) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);

        FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY);

        try {
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();

            builder.addBinaryBody("file", file, ContentType.DEFAULT_BINARY, fileBody.getFilename());
            requestParameters.forEach((key, value) -> builder.addTextBody(key, value, ContentType.TEXT_PLAIN));
            httpPost.setEntity(builder.build());

            CloseableHttpResponse response = httpClient.execute(httpPost);
            httpClient.close();
            return response;
        } catch (Exception e) {
            throw new RuntimeException("Uploading document failed, error: " + e.getMessage());
        }
    }

This is how I solved it:

private static HttpResponse<String> upload(String url, Map<String, String> requestParameters, File file) {
    try {
        Unirest.setTimeouts(0, 0);
        return Unirest.post(url)
                .field("Key", requestParameters.get("Key"))
                .field("X-Amz-Algorithm", requestParameters.get("X-Amz-Algorithm"))
                .field("X-Amz-Credential", requestParameters.get("X-Amz-Credential"))
                .field("X-Amz-Date", requestParameters.get("X-Amz-Date"))
                .field("X-Amz-Security-Token", requestParameters.get("X-Amz-Security-Token"))
                .field("Policy", requestParameters.get("Policy"))
                .field("X-Amz-Signature", requestParameters.get("X-Amz-Signature"))
                .field("X-Amz-Meta-X-Swan-Supporting-Document-Id", requestParameters.get("X-Amz-Meta-X-Swan-Supporting-Document-Id"))
                .field("file", file)
                .asString();
    } catch (Exception e) {
        throw new RuntimeException("Uploading document failed, error: " + e.getMessage());
    }
}

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