简体   繁体   中英

Spring Boot optional multipart POST request

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!

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 在此处输入图片说明

without image

在此处输入图片说明

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM