Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"
}
like image 341
krs8888 Avatar asked Dec 28 '25 07:12

krs8888


1 Answers

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 "[email protected]"

Refer post section of curl manual for more details

like image 165
Ravindra Devadiga Avatar answered Dec 30 '25 21:12

Ravindra Devadiga