Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSV on RequestBody using Spring

I need to receive a csv file on the request and process it.

@PostMapping(value = "/csv", consumes = "text/csv")
  public ViewObject postCsv(@RequestBody InputStream request){
// do stuff
}

But when I execute:

curl -X POST -H 'Content-Type: text/csv' -d @input.csv http://localhost:8080/csv

This is what shows up on my console:

Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'text/csv;charset=UTF-8' not supported]

Spring is saying my request is invalid before anything else.

So, the question is: How to properly receive this csv?

like image 850
kiosia Avatar asked Nov 07 '22 19:11

kiosia


1 Answers

As someone mentioned in comments, one approach is to implement your own HttpMessageConverter - there are examples of varying quality available on the web.

Another option if you already have some code for parsing CSVs using Spring's org.springframework.core.io.Resource classes is to change your method signature as follows to use Spring's built in ResourceHttpMessageConverter:

@PostMapping(path = "/csv", consumes = "application/octet-stream")
    public ViewObject postCsv(@RequestBody Resource resource){
    // do stuff
}
like image 194
Dave Avatar answered Nov 13 '22 16:11

Dave