2

I have a service where I want to be able to optionally upload a file (including a file will run a separate function) with a POST request.

A simplified version of what my ReqestMapping looks like is this:

@ApiOperation(value = "Data", nickname = "Create a new data object") @RequestMapping(value = "/add/{user_id}", produces = "application/json", method = RequestMethod.POST) public ResponseEntity<Data> addData(@RequestParam("note") String body, @RequestParam("location") String location, @RequestParam(value = "file", required = false) List<MultipartFile> file, @PathVariable String user_id){ if (file != null) { doSomething(file); } doRegularStuff(body, location, user_id); return new ResponseEntity(HttpStatus.OK); } 

As can be seen, I have the required = false option for my List of multipart files. However, when I attempt to curl the endpoint without any files and while stating that my content type is Content-Type: application/json, I get the error that my request isn't a multipart request.

Fine. So I change to Content-Type: multipart/form-data and without any files, I get the request was rejected because no multipart boundary was found (obviously, since I don't have a file).

This leads me to wonder how I can have a optional multipart parameter in my Spring endpoints? I would like to avoid having to add additional parameters to my request, such as "File Attached: True/False" as that can become cumbersome and unnecessary when the server can just check for existence.

Thanks!

1 Answer 1

4

There is no problem in your code, but the problem in client request, because Content-Type should be like below if you want to upload image,

multipart/form-data; boundary="123123" 

try to remove the Content-Type header and test, i will put one example for server code and client request

Server code:

@RequestMapping(method = RequestMethod.POST, value = "/users/profile") public ResponseEntity<?> handleFileUpload(@RequestParam("name") String name, @RequestParam(name="file", required=false) MultipartFile file) { log.info(" name : {}", name); if(file!=null) { log.info("image : {}", file.getOriginalFilename()); log.info("image content type : {}", file.getContentType()); } return new ResponseEntity<String>("Uploaded",HttpStatus.OK); } 

Client Request using Postman

with image enter image description here

without image

enter image description here

Curl example:

without image, with Content-Type

curl -X POST -H "Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW" -F "name=test" "http://localhost:8080/api/users/profile" 

without image, without Content-Type

curl -X POST -F "name=test" "http://localhost:8080/api/users/profile" 
Sign up to request clarification or add additional context in comments.

1 Comment

Could you possibly post a sample curl request with your answer for comparison as well? I'll test it out and accept your answer then. Thanks so much!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.