HTTP RFC2046 Request - Apache HTTPClient 4.5 Java - Mutipart/mixed content-type

608 Views Asked by At

I've been trying for a while now to generate an HTTP request as the one reported below with no luck, so I'm trying to get others' ideas from StackOverFlow.

The request should comprises a multi-part body, i.e. the HTTP Content-type header has a value of “multipart/mixed”, with an attachment of type octet-stream and a json body, see below:

· A JSON object with a Content-type of “application/json”;

· A binary object with a Content-type of “application/octet-stream”.

See below my current code and the output I'm getting on the server side, I've also attached the request I should produce. For the attachment I tried either FileBody and InputStreamBody with the same result.

On the server side I construct an HttpEntity which has no attachments but only the content reported below.

Hope someone out there will be able to give me an hint as that is becoming overwhelming.

Thanks in advance, Ettore.

 FileBody attacement = new FileBody(new File(url.getPath()));
 String requestBody = "{the json string}";

 HttpEntity entity = MultipartEntityBuilder 
                .create() 
                .addTextBody("jsonBody", requestBody, 
                                ContentType.create("application/json")) 
                .addPart("att", attacement).build(); 

HttpPost post = new HttpPost( 
                "http://localhost:10191/dummy/endpoint"); 
post.addHeader("Content-Type", "multipart/mixed"); 
post.setEntity(entity); 

HttpClient httpclient = HttpClientBuilder.create().build(); 

HttpResponse response = httpclient.execute(post); 

-----------------------------------Expected Request-----------------------------------------------------------------

POST firmware-url HTTP/1.1

Content-Length: nnn 

Content-Type: multipart/mixed; 

boundary=gzC2kvz70sKw8y5teEexPrJ6RB7PKgCbvXLpU 

--gzC2kvz70sKw8y5teEexPrJ6RB7PKgCbvXLpU 

Content-Type: application/json; charset=UTF-8


{ 
     "firmwareUpgrade": { 
           "requestId": "C2F440A248C84B919124DD8CCEBF8883", 
           "approvedFirmwareVersionId": "ABC123123", 
           "deliveryPoint": [ 
     {"commsHubId":“123346123”,"businessTargetId":"8E-62-33-A5-4F-00-44-65"}, 
     {"commsHubId":“3454567”,"businessTargetId":"85-03-E9-E5-7F-60-72-73"} 
           ] 
     } 
} 

--gzC2kvz70sKw8y5teEexPrJ6RB7PKgCbvXLpU 
Content-Type: application/octet-stream 

<Binary firmware image here>
--gzC2kvz70sKw8y5teEexPrJ6RB7PKgCbvXLpU--

--------------------------Sent to server-----------------------------------------------------------------------------

--io5JwmaTY0Tww98skQZjAjJamv71dD 
Content-Disposition: form-data; name="jsonBody" 
Content-Type: application/json 
Content-Transfer-Encoding: 8bit 

{
    "firmwareUpgrade": {
        "requestId": "123e4567-e89b-12d3-a456-426655440000",
        "approvedFirmwareVersionId": "ABC123123",
        "deliveryPoint": [
            {"commsHubId": 1854775807, "businessTargetId": "8E-62-33-A5-4F-00-44-65"},
            {"commsHubId": 1727580722, "businessTargetId": "85-03-E9-E5-7F-60-72-73"}
        ]
    }
}


--io5JwmaTY0Tww98skQZjAjJamv71dD 

Content-Disposition: form-data; name="att"; filename="firmwareDummyImage" 
Content-Type: application/octet-stream 
Content-Transfer-Encoding: binary 

fwefwerqfndsajncvdjakvn 
--io5JwmaTY0Tww98skQZjAjJamv71dD-- 

--------------------------Sent to server-----------------------------------------------------------------------------

0

There are 0 best solutions below