How to handle multipart/form-data with different content-types? - spring

How to parse multipart/form-data using Spring framework which has different content-types?
Content-Type: multipart/form-data; boundary=----------287032381131322
Content-Length: 999
----------287032381131322
Content-Disposition: form-data; name="targetDevices"
Content-Type: text/JSON
[ “12345678”, “22345678”, “32345678 ]
----------287032381131322
Content-Type:application/octet-stream
Content-Length:<file length>
<file goes here…>

Related

Google Drive API batch request with HTTP

these 2 docs confirm that (1) Google drive API can accept batch request, (2) currently Go-client does not have batch-request feature.
https://developers.google.com/drive/api/v3/batch#java
https://developers.googleblog.com/2018/03/discontinuing-support-for-json-rpc-and.html
I am looking at the example (https://developers.google.com/drive/api/v3/performance#example). I wonder if I can make a batch request with POSTMAN. How to do it? In this example (https://developers.google.com/drive/api/v3/performance#example), is everything below the first "--END_OF_PART" in the HTTP header? How to construct such a batch request in Postman or HTTP in general? Thanks a lot!
The example posted below as well.
POST https://www.googleapis.com/batch/drive/v3
Accept-Encoding: gzip
User-Agent: Google-HTTP-Java-Client/1.20.0 (gzip)
Content-Type: multipart/mixed; boundary=END_OF_PART
Content-Length: 963
--END_OF_PART
Content-Length: 337
Content-Type: application/http
content-id: 1
content-transfer-encoding: binary
POST https://www.googleapis.com/drive/v3/files/fileId/permissions?fields=id
Authorization: Bearer authorization_token
Content-Length: 70
Content-Type: application/json; charset=UTF-8
{
"emailAddress":"example#appsrocks.com",
"role":"writer",
"type":"user"
}
--END_OF_PART
Content-Length: 353
Content-Type: application/http
content-id: 2
content-transfer-encoding: binary
POST https://www.googleapis.com/drive/v3/files/fileId/permissions?fields=id&sendNotificationEmail=false
Authorization: Bearer authorization_token
Content-Length: 58
Content-Type: application/json; charset=UTF-8
{
"domain":"appsrocks.com",
"role":"reader",
"type":"domain"
}
--END_OF_PART--

Parsing multipart/mixed responses

I want to parse a multipart/mixed web response I'm getting in to proper content,
--Boundary_33046_835445861_1627668603035
Content-Type: application/json
Content-Disposition: form-data; name="contentAnalyzerResponse"
{"cpf:inputs":{"documentIn":{"cpf:location":"InputFile0","dc:format":"application/pdf"},"params":{"cpf:inline":{"elementsToExtract":["text","tables"],"renditionsToExtract":["tables","figures"]}}},"cpf:engine":{"repo:assetId":"urn:aaid:cpf:58af6e2c-1f0c-400d-9188-078000185695"},"cpf:status":{"completed":true,"type":"","status":200},"cpf:outputs":{"elementsRenditions":[{"cpf:location":"fileoutpart0","dc:format":"image/png"},{"cpf:location":"fileoutpart1","dc:format":"image/png"},{"cpf:location":"fileoutpart2","dc:format":"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},{"cpf:location":"fileoutpart3","dc:format":"image/png"},{"cpf:location":"fileoutpart4","dc:format":"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},{"cpf:location":"fileoutpart5","dc:format":"image/png"},{"cpf:location":"fileoutpart6","dc:format":"image/png"},{"cpf:location":"fileoutpart7","dc:format":"image/png"},{"cpf:location":"fileoutpart8","dc:format":"image/png"},{"cpf:location":"fileoutpart9","dc:format":"image/png"},{"cpf:location":"fileoutpart10","dc:format":"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},{"cpf:location":"fileoutpart11","dc:format":"image/png"},{"cpf:location":"fileoutpart12","dc:format":"application/vnd.
--Boundary_33046_835445861_1627668603035
Content-Type: application/octet-stream
Content-Disposition: form-data; name="fileoutpart92"
�PNG
�޾�L���޺S�|��wp���F��k�^����{��{�d\'�����|v������Q<H=||�϶�7����yHBz��q���o݉մ�\:���/�[Ó���:�� ����Tկ�����K�/|g��HY��F�����/��ݳ/�7�6�w$�����񉟏�F�򁱑�sZ�0T�I��p���0�힍��:�J�����_��>���k��9~���C�P��Pl3���K\�:�ʵ����?z��G�w�V��_yg/�������~Z_3��W����x�[���N�9U}�E�~|�g��:�u��m�B��ZQ����z�O��=W�=gܪ�|x�2����o|pU��/���~�����ޒ������}����}�W���v���k��pM�yI��I<�t�w��
���}~�����M<����7~�c�l���gF��?���o�!` ��0�G�ioG.ީ������^��S�96Ź��a8K�{�N�paa8�<�1���p�]؝�>�x�#Y�z���y�����w>s�K���c�5��pQ�b����i���3�8���������&�h>?wo���{���WjɅO݉�T�w���8��:�ut�̩�Eu����&^���>sʸmk<������1
��oB"�0� �������ƾ��ܴ�a8c�u�~��<%0
The response I'm getting is similar to the responses above and I want to get the individual content from the response.

Spring boot with Apache CXF Multipart request always fails with 400

I am using spring boot along with Apache CXF. My REST API definition looks as below
 
#PUT
#Consumes(value = {MediaType.MULTIPART_FORM_DATA})
#ApiOperation(value = "Sends a notification")
public boolean sendNotification(
#FormParam(QueryParams.SUBJECT) String subject,
#FormParam(QueryParams.MESSAGE) String message) {
}
When we hit via REST client, we are always getting 400.
PUT /rest/notifications HTTP/1.1
Host: localhost:9080
User-Agent: insomnia/2020.3.3
Content-Type: multipart/form-data; boundary=X-INSOMNIA-BOUNDARY
Accept: /
Content-Length: 231
--X-INSOMNIA-BOUNDARY
Content-Disposition: form-data; name="subject"
A new topic has been created
--X-INSOMNIA-BOUNDARY
Content-Disposition: form-data; name="message"
A new topic has been created
--X-INSOMNIA-BOUNDARY--
We are completely uploaded and fine
Mark bundle as not supporting multiuse
HTTP/1.1 400
Content-Encoding: UTF-8
Date: Tue, 04 Aug 2020 05:08:42 GMT
Content-Type: text/plain
Content-Length: 50
Connection: close

How to uplaod a file to a multipart post request in ruby

I am trying to upload a file(huge) to a multipart post request in ruby. I use the following code but it gives me 500 internal server error:
RestClient.post("urladdress',
:name_of_file_param => File.new('path_of_the_file_to_be_uploaded'))
Following is the multipart request from the endpoint I am using currently:
POST address HTTP/1.1
Host: ip address
Content-Length: 325
Cache-Control: max-age=0
Origin: http://address
Upgrade-Insecure-Requests: 1
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryol2BpbpZ9cZlqFyb
User-Agent: blah blah blah
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Referer: http://address
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
------WebKitFormBoundaryol2BpbpZ9cZlqFyb
Content-Disposition: form-data; name="uploadedFile"; filename="blah.csv"
Content-Type: text/csv
123456789010290
123456789010290
------WebKitFormBoundaryol2BpbpZ9cZlqFyb
Content-Disposition: form-data; name="submit"
Upload APK
------WebKitFormBoundaryol2BpbpZ9cZlqFyb--
Also if someone can help me with creating a new endpoint which can be used to upload the file without using multipart that is fine too.
Thanks,
RV

Using CommonsMultipartResolver to parse an HTTP request with embedded multipart content

I am using Spring Integration to receive an HTTP request which has multipart content nested inside a multipart request as shown below. When using CommonsMultipartResolver as the resolver the LinkedMultiValueMap passed to the receiving service activator contains a map of all the parts but the imbedded multipart is an UploadedMultipartFile which contains the entire content. In the sample boundary2----extKDvsqGExTQI2WVuplHFCUQJ3XVL is the boundary of the embedded multipart.
How can I get the embedded multipart parsed into a LinkedMultiValueMap so that I can get the version and payload? Is it possible for the CommonsMultipartResolver to do this or is there another way to parse it?
Http Request:
POST http://xxxxx:8087/edi846Inbound HTTP/1.1
Content-Type: multipart/form-data;boundary=DM1-Sb0636VXPQNO3412Ygegb9suqr
.......
Content-Length: 2787
--DM1-Sb0636VXPQNO3412Ygegb9suqr
Content-Disposition: form-data; name="from"
Content-Type: text/plain;charset=ISO-8859-1 Content-Length: 7
gisb-acknowledgement-receipt
--DM1-Sb0636VXPQNO3412Ygegb9suqr
Content-Disposition: form-data; name="input-format" Content-Type:
text/plain;charset=ISO-8859-1 Content-Length: 3
X12
--DM1-Sb0636VXPQNO3412Ygegb9suqr
Content-Disposition: form-data; name="input-data"; filename="TEST-DTEBS_846_3563_10142015102600.x12"
Content-Type:
multipart/encrypted;boundary=boundary2--extKDvsqGExTQI2WVuplHFCUQJ3XVL protocol:
application/pgp-encrypted
boundary2----extKDvsqGExTQI2WVuplHFCUQJ3XVL
Content-Disposition: form-data; name="version" Content-Type:
application/pgp-encrypted Content-Length: 10
Version: 1
boundary2----extKDvsqGExTQI2WVuplHFCUQJ3XVL
Content-Disposition: form-data; name="payload" Content-Type: application/octet-stream Content-Length:
1118
-----BEGIN PGP MESSAGE-----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--
--DM1-Sb0636VXPQNO3412Ygegb9suqr--
Embedded content from Map:
boundary2----extKDvsqGExTQI2WVuplHFCUQJ3XVL
Content-Disposition: form-data; name="version" Content-Type: application/pgp-encrypted Content-Length:
10
Version: 1
boundary2----extKDvsqGExTQI2WVuplHFCUQJ3XVL
Content-Disposition: form-data; name="payload" Content-Type: application/octet-stream Content-Length:
1118
-----BEGIN PGP MESSAGE----- Version: BCPG v1.47
hQIOA8aa0/bci2e/EAf/fkgOBK+cOoeY+44htcvjE9Sxk9RK73aOEaxDOBNqFOsV
Nzw1va7cc7wzYOTlHhZV/MkpXq+IrZV316moJu1EAhscN9YmyReVDOzrkzZ1hBmq
V2MlAr5/CRqS7nQEmCqy/Mz0STtAPZNg3FOh8F79dJnRws5/SdW+zvn2MoSvGTs3
0YvjmtPq2YyVkMlAvJ62f903UYNIiAcEe1k8gWDVW9LJ4tGud+8H0bsG8EFWPsSI
b5rON+MKGykbWGnbSrOy9cqslJaghj91r7ZbsXXXiRn9v4rpOardfiBEnoamI5Tz
jfDKESnr7CqoZgb62je+NJrB5puoAwy8LTynXx4cmQgAnf8lb6f+mIxa1U1Mu6iR
KNTjrH83Cx2BjTra8E8hu0O7GvuczGfA9NiONmxbglgKK58/QVYdlsZUiGTaWkkY
4nMHiaFe9TiPQ6W2vMGutLFR3V6YSshchCskl6cP1OdaIXCYgzZKIjUWHePzd/ds
zCZeNqul1N7iIQ0L5lfSGgfB2eDeeK3JKFjetrrbJQXz0+XeTZLPMSMr6W5Ggq3l
PnoJVTrxUd3b2WKgR4FZB+imYRWEz5KLnB5ZVedC+fU24bECcNptrTQaZZUWnR3Z
ABJqz8avBrNV0Jf9BaFRxcWkrWyr4KtxDArxXOuMHNvGVtj+hhPZcPGj3N9Xam3E
6snAGebIiAEivZPYLVVVk0lZW2H0ISwkKkjD8qs7h8wa7Qbchp2K5IyENEzi20oe
FNRoCwHyjsCY0nECHCG3WON0EXG2DZDTvA17OoyfuKnRT09FVt3OOEPTV048vUid
kUoGa7uT7ud9En7uNSfmDpw+xHxB38N986BKBtoeJlGgDYISJkIgr40TIKMLE/Yc
whDnmKxT68dfSGcv/DIUzYs+d/OBzSpHIWK87me5QnhmLTQ1xwfwtFoTyk6B3Vzx
ljlZFD6e7PJfjo/RIhb0zMvtBvRTONRrMXKRdRY=
=serM
-----END PGP MESSAGE-----
boundary2----extKDvsqGExTQI2WVuplHFCUQJ3XVL--
Not sure what you mean, but I'd suggest to DEBUG the code in the MultipartAwareFormHttpMessageConverter.readMultipart(). That is a place where an UploadedMultipartFile appears.
A quick look at the CommonsMultipartResolver seems to indicate it doesn't support nested multiparts.
It might not be too hard to subclass it and override parseRequest(), but you might have to dig into commons-fileupload as well.
EDIT
Regarding multipart response:
I just patched the http sample and found that, as long as the request has a proper accept header, simply returning a MultiValueMap "just works".
If you need more control over the part headers, you would need to subclass the AllEncompassingFormHttpMessageConverter and override getHttpEntity() to insert extra headers and configure the gateway to use the custom adapter. Basic headers work ok, though - this...
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
map.add("foo", "Hello");
map.add("bar", "baz");
map.add("fiz", "qux".getBytes());
return map;
...results in this...
--6_zk8PXoaiUO_By2XJuUpjs5CEEnYM-wj0qE
Content-Disposition: form-data; name="foo"
Content-Type: text/plain;charset=ISO-8859-1
Content-Length: 5
Hello
--6_zk8PXoaiUO_By2XJuUpjs5CEEnYM-wj0qE
Content-Disposition: form-data; name="bar"
Content-Type: text/plain;charset=ISO-8859-1
Content-Length: 3
baz
--6_zk8PXoaiUO_By2XJuUpjs5CEEnYM-wj0qE
Content-Disposition: form-data; name="fiz"
Content-Type: application/octet-stream
Content-Length: 3
qux
--6_zk8PXoaiUO_By2XJuUpjs5CEEnYM-wj0qE--

Resources