I need to send request. It looks like this cURL request:
curl --location --request POST 'http://127.0.0.1:8080/api/attachments'
--form 'data=@path/to/file/6C233C5F-B60D-41CA-86ED-A43C51FD7EED.png'
If to convert it to IDEA http request, it shall look like here:
POST http://127.0.0.1:8080/api/attachments
Content-Type: multipart/form-data; boundary=WebAppBoundary
--WebAppBoundary
Content-Disposition: form-data; name="data"; filename="6C233C5F-B60D-41CA-86ED-A43C51FD7EED.png"
< path/to/file/6C233C5F-B60D-41CA-86ED-A43C51FD7EED.png
--WebAppBoundary--
Ok, but I need to send it with restTemplate
. And it's desirable to use byte[]
instead of file path. Please, help.
Here is my code fragment
HttpEntity<?> entity;
HttpHeaders headers = new HttpHeaders();
Object requestBody;
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
requestBody = Base64.getDecoder().decode(base64EncodedData);
entity = new HttpEntity<>(requestBody, headers);
ResponseEntity response = getRestTemplate().exchange(
serviceURL,
HttpMethod.POST,
entity,
String.class
);
You can read file content to byte array and send it the same way.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.