Is there an easy way to process the body of a POST HTTP request as an InputStream when using PostMapping in a Spring Boot RestController?
It is quite simple to accept file uploads from Multipart HTTP POST requests as MultipartFile instances, but I would like to be able to simply post binary content to an HTTP endpoint and process this as an InputStream.
Is this possible with Spring Boot?
For example with the following Postman POST:
I know two possible ways
Take HttpEntity
@PostMapping
public ResponseEntity<String> post(HttpEntity<byte[]> requestEntity) {
return ResponseEntity.ok(new String(requestEntity.getBody()));
}
Take whole Request
@PostMapping
public ResponseEntity<String> post(HttpServletRequest request) {
request.getInputStream();
}
If you just have an InputStream
parameter on your method then you get the request body.
@RequestMapping(method = RequestMethod.PUT)
public ResponseEntity<Void> put(InputStream is) {
// Do something
return ResponseEntity.ok();
}
This is with Spring Boot 2.4.1.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With