Reputation: 21
I used following code to consume multipart/form-data
. But there I can't specify content-type
for files. How to pass content-type
, filename, filename content in spring?
HttpHeaders header = new HttpHeaders();
header.add("Token", "_45378ffb-e366-45ec-9ac4-eb968c57aee3"); header.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, Object> fileMap = new LinkedMultiValueMap<>();
fileMap.add(filename, msg.getBytes());
fileMap.add("OrderRequest", message);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(fileMap, header);
ResponseEntity<String> response = restTemplate.postForEntity(url, requestEntity,String.class);
I used MultiValueMap to add filename and its content and i am not able to pass content-type of file. When executed I get 400 Bad requests. Can you help here?
I have tried previously as below, here also getting 400 bad requests.Need help here.
HttpHeaders headers = new HttpHeaders();
headers.add("token", "_fd470451-cb5d-4fa2-8e18-0bf8a7a92d04");
headers.add("Content-Type", "multipart/form-data; boundary=\"---boundary\"");
MultiValueMap<String, Object> fileMap = new LinkedMultiValueMap<>();
ContentDisposition contentDisposition = ContentDisposition.builder("form-data").name(filename).filename(filename).build();
fileMap.add(contentDisposition.toString() + "\n" + " Content-Type: application/octet-stream", msg);
ContentDisposition contentDisposition1 = ContentDisposition.builder("form-data").name("OrderRequest").filename("OrderRequest").build();
fileMap.add(contentDisposition1.toString(), message);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(fileMap,headers);
ResponseEntity<String> response = restTemplate.postForEntity(url, requestEntity, String.class);
Upvotes: 1
Views: 4961
Reputation: 21
Thank you, I tried but again am getting 400 bad request. here I used tcpdump and got response like this:
POST XXXXXXXXXXXXXXXXXXXXX
Accept: text/plain, application/json, application/*+json, */*
Content-Type: multipart/form-data;charset=UTF-8;boundary=XXXXXXXXXXXXXXXX
Token: XXXXXXXXXXXXXXXXXXXXXXXXXXX
User-Agent: Java/1.8.0_201
Host: XXXXXXXXXXXXXXXXXXXXXXXXXX
Connection: keep-alive
Content-Length: 1003
�bA]fG
�k�?A�--XXXXXXXXXXXXXXXXXXXX
Content-Disposition: form-data; name=filename; filename=fileName
Content-Type: application/octet-stream
Content-Length: 511
MSH|^~\&|GHHHH v2012007|HL93765411|GHH|DHM|20170725121244||ORM^O01|ghjk09876|P|2.3.1|6||AL|NE|AU|||
PID|1|||11057^^^GPC Test Practice^MR^GPC Test Practice|XXX^XXX^^^jjj^^L||19920321|F|||188 HEIGHTS DR^^ROBINA^QLD^4226^^C|||^ORN^PH^^^^04
01817188^||||||||||||||||
PV1|1|O||||||2121331W^BHAR^NAV^^^MR.^^^AUSHICPR|2121331W^BHAR^NAV^^^MR.^^^AUSHICPR|||||||||||
ORC|NW|10188-1||10188||||||||^Admin^Mr.
OBR|1|10188-1||T001^Histopathology^LN|||201707251212||||L|||||^Admin^Mr.||||||||LAB
BLG||F
--XXXXXXXXXXXXXXXXXX
Content-Disposition: form-data; name="FileName1"
Content-Type: application/json
Content-Length: 133
{"client_software_version":"XXX","batch_id":"XXXXX","order_files_sent":["fileName"],"client_software_name":"XXXXX"}
--XXXXXXXXXXXX--
Below response is the expected response for rest template. Need help here. Its working for the below tcpdump reponse and reponse which we got without using rest template is:
POST XXXXXXXXXXXXXXXXX
token: XXXXXXXXXXXXXXXXXXXXXXXXXX
content-type: multipart/form-data; boundary="---boundary"
Content-Length: 940
Host: XXXXXXXXXXXXXXXXXXXXXXXXXXXX
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.5.3 (Java/1.8.0_201)
Accept-Encoding: gzip,deflate
-----boundary
Content-Disposition: form-data; name="fileName"; filename="fileName"
Content-Type: application/octet-stream
MSH|^~\&|GHHHH v2012007|HL93765411|GHH|DHM|20170725121244||ORM^O01|ghjk09876|P|2.3.1|6||AL|NE|AU|||
PID|1|||11057^^^GPC Test Practice^MR^GPC Test Practice|XXX^XXX^^^jjj^^L||19920321|F|||188 HEIGHTS DR^^ROBINA^QLD^4226^^C|||^ORN^PH^^^^04
01817188^||||||||||||||||
PV1|1|O||||||2121331W^BHAR^NAV^^^MR.^^^AUSHICPR|2121331W^BHAR^NAV^^^MR.^^^AUSHICPR|||||||||||
ORC|NW|10188-1||10188||||||||^Admin^Mr.
OBR|1|10188-1||T001^Histopathology^LN|||201707251212||||L|||||^Admin^Mr.||||||||LAB
BLG||F
-----boundary
Content-Disposition: form-data; name="filename1"; filename="filename1"
Content-Type: application/json; charset=UTF-8
{"client_software_version":"XXX","batch_id":"XXXXX","order_files_sent":["fileName"],"client_software_name":"XXXXX"}
-----boundary--
Upvotes: 1
Reputation: 7521
Prepare file part
HttpHeaders filePartHeaders = new HttpHeaders();
filePartHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
HttpEntity filePart = new HttpEntity<>(new ByteArrayResource(msg.getBytes()) {
@Override
public String getFilename() {
return filename;
}
}, filePartHeaders);
Then prepare message part
HttpHeaders messagePartHeaders = new HttpHeaders();
messagePartHeaders.setContentType(MediaType.TEXT_PLAIN);
HttpEntity messagePart = new HttpEntity<>(message, messagePartHeaders);
Then combine parts into a multipart request entity
HttpHeaders header = new HttpHeaders();
header.add("Token", "_45378ffb-e366-45ec-9ac4-eb968c57aee3");
header.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, Object> multiMap = new LinkedMultiValueMap<>();
multiMap.add(filename, filePart);
multiMap.add("OrderRequest", messagePart);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(multiMap, header);
And finally perform request
ResponseEntity<String> response = restTemplate.postForEntity(url, requestEntity, String.class);
Upvotes: 2