Spring Boot RestController - PostMapping - Process Request Body as InputStream

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:

enter image description here

1 answer

  • answered 2020-01-18 11:28 Matthias

    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();
    }