I'm trying to create a rest endpoint with java spring that accepts a csv file.
My Controller looks like this:
Interface:
package my.company.my.project.trms.controller;
import my.company.my.project.trms.controller.common.ControllerUrls.INBOX.CSV;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@RestController
@RequestMapping(CSV.BASE)
public interface CsvController {
@PostMapping(produces = "text/csv", consumes = "text/csv")
public ResponseEntity create(@RequestBody MultipartFile file);
}
*CSV.BASE is a static final String holding my endpoint url
Implementation:
package my.company.my.project.trms.controller;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@Slf4j
@RequiredArgsConstructor
@RestController
public class CsvControllerImpl implements CsvController {
@Override
public ResponseEntity create(MultipartFile file) {
String message = "";
return ResponseEntity.status(HttpStatus.OK).body(message);
}
}
I want to test this endpoint with the folowing sh script executet on a windows pc using git bash:
#!/bin/bash
curl -X POST "http://localhost:8791/api/public/v1/inboxes/csv" -H "accept: */*" -H "Content-Type: text/csv" --data-binary @/c/Users/Schilling/Desktop/Test.csv
When I execute the script my controller Method is called. However, setting a breakpoint shows me that the parameter "file" is always null.
I suspect there is something worng with the syntax of the file Path in the curl script, therefore I tried several things including absolute and relative paths. Of cause the error could also originate from my controller class.
EDIT:
Adding the -vv option to the curl call resulted in this output:
Note: Unnecessary use of -X or --request, POST is already inferred.
* Uses proxy env variable no_proxy == '192.168.99.100'
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Trying 127.0.0.1:8791...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8791 (#0)
> POST /api/public/v1/inboxes/csv HTTP/1.1
> Host: localhost:8791
> User-Agent: curl/7.65.3
> accept: */*
> Content-Type: text/csv
> Content-Length: 2036
> Expect: 100-continue
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 100
} [2036 bytes data]
* We are completely uploaded and fine
100 2036 0 0 100 2036 0 221 0:00:09 0:00:09 --:--:-- 0* Mark bundle as not supporting multiuse
< HTTP/1.1 200
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: 0
< X-Frame-Options: SAMEORIGIN
< Content-Type: text/csv;charset=UTF-8
< Content-Length: 0
< Date: Fri, 15 Oct 2021 12:37:43 GMT
<
100 2036 0 0 100 2036 0 203 0:00:10 0:00:09 0:00:01 0
* Connection #0 to host localhost left intact
CodePudding user response:
I think this should work as it needs to be: Content-Type: multipart/form-data according to the code.
#!/bin/bash
curl -vv -F upload=@/c/Users/Schilling/Desktop/Test.csv "http://localhost:8791/api/public/v1/inboxes/csv"