Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get raw binary data from a POST request processed by Spring?

I need to write an application which would be able to process binary data sent by CUrl, such as:

curl localhost:8080/data --data-binary @ZYSF15A46K1.txt

I've created a POST processing method as follows:

@RequestMapping(method = RequestMethod.POST, value = "/data")
    public void acceptData(HttpEntity<byte[]> requestEntity) throws Exception {
        process(requestEntity.getBody());
    }

However it doesn't seem to be returning raw binary data. I've tried sending a GZip file and after going through Spring it is now longer decompressible, which leads me to believe I'm either getting too much data or too little data.

How do I solve this issue and get raw binary data?

like image 325
JonathanReez Avatar asked Apr 27 '16 22:04

JonathanReez


People also ask

How is binary data passed in the postman?

First, set the body to "binary": Select "binary" to have Postman send binary data. Now click "Select File" in Postman to attach a file: Attach a file to upload with the "Select File" button.

Which method is suitable for passing binary data like images documents?

JSON / XML Probably the easiest and most compatible way to send the data is to serialize it to JSON or XML. Also the binary data, which means getting +33% in message size due to BASE64 compression. This may be just fine in some cases.

What is binary raw data?

In this case "raw binary" means raw bytes. A SHA-1 digest is 160 bits long, or 20 bytes. Many systems use the ASCII presentation format, which is 40 hexadecimal characters, but sometimes you need the raw data.

What is get and post method in spring boot?

GET and POST Two common methods for the request-response between a server and client are: GET- It requests the data from a specified resource. POST- It submits the processed data to a specified resource.


2 Answers

It's as easy as declaring an InputStream in your controller method's parameters:

@RequestMapping(method = RequestMethod.POST, value = "/data")
public void acceptData(InputStream dataStream) throws Exception {
    processText(dataStream);
}

You shouldn't need to disable HiddenHttpMethodFilter, if you do it's probably that your request is wrong in some way. See https://github.com/spring-projects/spring-boot/issues/5676.

like image 52
nimai Avatar answered Oct 16 '22 18:10

nimai


I was able to resolve this using the following code:

@Bean
public FilterRegistrationBean registration(HiddenHttpMethodFilter filter) {
    FilterRegistrationBean registration = new FilterRegistrationBean(filter);
    registration.setEnabled(false);
    return registration;
}

@RequestMapping(method = RequestMethod.POST, value = "/data")
public void acceptData(HttpServletRequest requestEntity) throws Exception {
    byte[] processedText = IOUtils.toByteArray(requestEntity.getInputStream());
    processText(processedText);
}

Spring does pre-processing by default, which causes the HttpServletRequest to be empty by the time it reaches the RequestMapping. Adding the FilterRegistrationBean Bean solves that issue.

like image 25
JonathanReez Avatar answered Oct 16 '22 18:10

JonathanReez