CloseableHttpClient with BasicAuth creds returning 400 error. With same BasicAuth creds OkHttp3 client works well

Viewed 19

I have the sample code snippet. It is returning 400 error. With same BasicAuth creds using OkHttp3 client works well. What is missing here?

```
    String BASIC_AUTHORIZAION = "Basic *********"; // masked
    
    String reportRequest = "{***}"; //json string

    CloseableHttpClient client = HttpClientBuilder.create().build();

    FormBodyPart bodyPart = FormBodyPartBuilder
        .create()
        .setName("ReportRequest")
        .setBody(new StringBody(reportRequest, ContentType.APPLICATION_JSON))
        .build();

    HttpEntity requestEntity = MultipartEntityBuilder
        .create()
        .addPart(bodyPart)
        .addTextBody("type", ContentType.APPLICATION_JSON.toString())
        .build();

    HttpUriRequest httpPost = RequestBuilder
        .post(MPG_MESSAGE_STATUS_REPORT_URL)
        .addHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString())
        .addHeader("Authorization", BASIC_AUTHORIZAION)
        .setEntity(requestEntity)
        .build();

    HttpResponse response = client.execute(httpPost);
1 Answers

Resolved as it does not need the line to add header for Content-Type MULTIPART_FORM_DATA:

 .addHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString())
Related