How to create multipart/mixed POST request body for Olingo OData4 while Batch processing of file upload?

105 Views Asked by At

I'm able to use multipart request body for file upload using Postman for Apache Olingo Odata batch processing. However, looking to generate same request using Olingo client APIs. That is, for example taking an analogy of Apache HTTPClient, there is a MultipartEntityBuilder which does the needful, looking for same with Olingo Odata4 client.

Below Batch request works fine in Postman:

https://hostname:443/MyApp/servlet/odata/MyDomain/$batch
....
Content-Type multipart/mixed;boundary=batch_50f02a08-698b-4af0-b19a-1fcb75d244d8

Raw request body used in Postman:

--batch_50f02a08-698b-4af0-b19a-1fcb75d244d8
Content-Type: application/http
Content-Transfer-Encoding:binary
Content-ID:1

PUT https://hostname:443/MyApp/servlet/odata/MyDomain/Documents('OID:my.doc.Document:12345')/MyContent/$value HTTP/1.1
Accept: */*
Content-Type: multipart/form-data; boundary=ContentSeparator
OData-MaxVersion: 4.0
OData-Version: 4.0
Content-ID: 1

--ContentSeparator
Content-Disposition: form-data; name="myFilepathInput";filename="myFile1.txt"
Content-Type: text/plain

Hello, World!
This is a text file.

--ContentSeparator--

--batch_50f02a08-698b-4af0-b19a-1fcb75d244d8--

In order to generate above raw request body, I tried below 2 approaches but the problem is such that I'm not able to generate a boundary separated part(below) in the body:

--ContentSeparator
Content-Disposition: form-data; name="myFilepathInput";filename="myFile1.txt"
Content-Type: text/plain

Hello, World!
This is a text file.

--ContentSeparator--

1) Using ODataEntityUpdateRequest. Pseudo code to generate request body is below:

ODataClient client = ODataClientFactory.getClient();
ODataBatchRequest req = client.getBatchRequestFactory().getBatchRequest(batchBaseURI.toString());
BatchManager payloadManager = batchRequest.payloadManager();
//Unnecessary lines removed..
File file = new File(filepath.toString());
ClientEntity entity = client.getObjectFactory().newEntity(qualifiedName);
//Unnecessary lines removed...
entity.setMediaContentSource(file.toURI());
entity.setMediaContentType(ContentType.parse("text/html").toContentTypeString());
entity.setMediaEntity(true);

URI insertUri = //A PUT request URI. Unnecessary lines removed...

CUDRequestFactory requestFactory = client.getCUDRequestFactory();
ODataEntityUpdateRequest<ClientEntity> updateRequest = requestFactory.getEntityUpdateRequest(insertUri, UpdateType.REPLACE, entity);
updateRequest.setContentType("multipart/form-data");
//Unnecessary lines removed...
payloadManager.addRequest(updateRequest);

This gives me request body as below with file information in JSON format:

--batch_a4ac4cb0-c851-4767-a043-e7df8b24e0fb
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID:1

PUT https://hostname:443/MyApp/servlet/odata/MyDomain/Documents('OID:my.doc.Document:12345')/MyContent/$value HTTP/1.1
Accept: */*
Content-Type: multipart/form-data
OData-MaxVersion: 4.0
OData-Version: 4.0
Content-ID: 1

{"FileName":"myFile1.txt","filePath":"D:/temp/myFile1.txt"}
--batch_a4ac4cb0-c851-4767-a043-e7df8b24e0fb—

2) Using ODataMediaEntityUpdateRequest. Pseudo code to generate request body is below:

ODataClient client = ODataClientFactory.getClient();
InputStream targetStream = new FileInputStream(file);
ClientEntity entity = client.getObjectFactory().newEntity(qualifiedName);
//Unnecessary lines removed...

URI insertUri = //A Put request URI for PrimaryContent, highlighted in Green in below request body. Unnecessary lines removed...

CUDRequestFactory requestFactory = client.getCUDRequestFactory();
       ODataMediaEntityUpdateRequest<ClientEntity> updateMediaRequest = requestFactory.getMediaEntityUpdateRequest(insertUri, targetStream);
       updateMediaRequest.setContentType("multipart/form-data");
payloadManager.addRequest(updateMediaRequest);

This gives me request body as below with file contents, But without file related information and a separate boundary/part:

--batch_fc2c9fc0-4a9b-44cf-a7c1-84aabff2e106
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID:1

PUT https://hostname:443/MyApp/servlet/odata/MyDomain/Documents('OID:my.doc.Document:12345')/MyContent/$value HTTP/1.1
Accept: */*
Content-Type: multipart/form-data
OData-MaxVersion: 4.0
OData-Version: 4.0
Content-ID: 1

Hello, World!
This is a text file.
--batch_fc2c9fc0-4a9b-44cf-a7c1-84aabff2e106--
0

There are 0 best solutions below