krs8888 krs8888 - 2 months ago 74
reST (reStructuredText) Question

spring-boot REST POST API to send a file

I am new to spring rest and trying to create a REST POST API where the user can send a file to the server.

@RequestMapping(value = "/order", method = RequestMethod.POST)
public String create(@RequestParam("file") MultipartFile file) {
System.out.println("---------INSIDE ORDER----------");
return "file succesfully received!";
}


But I when make a call to this API by uploading a order.txt file and selecting form-data (in postman) I get this error

{
"timestamp": 1474129488458,
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.web.multipart.support.MissingServletRequestPartException",
"message": "Required request part 'file' is not present",
"path": "/order"
}

Answer

Problem is not with your code which accepts the request. It is with the request how you are making.

-d is used to pass the data. You have to use -F as shown below

curl -X POST localhost:8080/order -F "file=@cooltext.txt"

Refer post section of curl manual for more details