kagmole
kagmole

Reputation: 2165

Form object with @RequestPart parameter results in 404

I am trying to create an URI where both a MultipartFile and a form object (bean) are required.

The method worked with the following code:

@RequestMapping(method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public void create(@RequestPart MultipartFile file,
                   @RequestPart String form) {
    // ...
}

And the following curl command:

curl -X POST http://localhost/files \
  -F file=@E:\path\to\file \
  -F form={"x":0,"y":0,"width":512,"height":512}

But as soon as I try to replace the string by a bean, I get a 404 response.

Modified code, not working, with a bean:

@RequestMapping(method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public void create(@RequestPart MultipartFile file,
                   @RequestPart ClipForm form) {
    // ...
}

Adding the Content-Type header for each part in curl does not seem to help:

curl -X POST http://localhost/files \
  -F file=@E:\path\to\file;type=multipart/form-data \
  -F form={"x":0,"y":0,"width":512,"height":512};type=application/json

I am probably missing something that makes Spring think the request is not mapped to this method, but so far I cannot get what.

Upvotes: 0

Views: 1830

Answers (1)

kagmole
kagmole

Reputation: 2165

For whatever reason, quoting everything in the curl command and using bash made this working.

curl -X 'POST' \
     -F 'file=@E:\path\to\file;type=multipart/form-data' \
     -F 'form={"x":0,"y":0,"width":512,"height":512};type=application/json' \
     'http://localhost/files'

I guess a character was misinterpreted by the Windows cmd.


EDIT

If you want it to get working in the Windows cmd, you will need to use double quotes and escaped double quotes. It will not use the single quote like bash.

curl -X "POST" ^
     -F "file=@E:\path\to\file;type=multipart/form-data" ^
     -F "form={\"x\":0,\"y\":0,\"width\":512,\"height\":512};type=application/json" ^
     "http://localhost/files"

Upvotes: 1

Related Questions