1

I want to send multi part form data to my spring boot rest controller.

Below is my code of request handler

@PostMapping(value = "/postmultipartformdata" , consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public String postFormData(@RequestBody MultiValueMap<String, String> formData) {
    
    return "Welcome to the post method with multi part form data. Printing whatever is present in the body " +  formData;
}

However whenever i send the request with form data from postman. I get this response back

  "timestamp": "2020-07-02T05:10:40.320+00:00",
"status": 415,
"error": "Unsupported Media Type",
"message": "",
"path": "/postmultipartformdata"

Probably i am missing something very simple.

Best Regards,

Saurav

saurav
  • 5,388
  • 10
  • 56
  • 101
  • 1
    Did you check https://stackoverflow.com/questions/16015548/tool-for-sending-multipart-form-data-request – Ori Marko Jul 02 '20 at 05:21

3 Answers3

1

You need to use @RequestParam instead of @RequestBody and also if the content that you are sending is actually a file , map it to a MultipartFile object, which in your case might not be needed but since it is not clear from your question the kind of data that you are sending I am including this here, like :

@PostMapping(value = "/postmultipartformdata" , consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public String postFormData(@RequestParam("file") MultipartFile file,
        RedirectAttributes redirectAttributes) {

    yourService.store(file);
    redirectAttributes.addFlashAttribute("message",
            "You successfully uploaded " + file.getOriginalFilename() + "!");

    return "redirect:/";
}

Usually we use MultipartFile as a representation of an uploaded file received in a multipart request.

Ananthapadmanabhan
  • 5,706
  • 6
  • 22
  • 39
1

You could also use @RequestPart instead of @RequestBody to obtain the wanted part in your multipart request.

So like:

@RequestPart LinkedMultiValueMap<String, String> formData

I also switched to concrete class LinkedMultiValueMap since Spring had hard times to instantiate MultiValueMap.

Then send the formData part with Content-Type: application/json.

pirho
  • 11,565
  • 12
  • 43
  • 70
0

You have to use @RequestParam instead of @RequestBody. Since it is form data is treated with request param and not with the request body.

Here is how I have corrected it:

@PostMapping(value = "/postmultipartformdata", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String postFormData(@RequestParam MultiValueMap<String, String> formData) {
        return "Welcome to the post method with multi part form data. Printing whatever is present in the body " + formData;
}

command

curl --location --request POST 'http://localhost:8080/status/postmultipartformdata' \
--form 'a=a' \
--form 'b=b' \
--form 'c=c'

output:

Welcome to the post method with multi part form data.
Printing whatever is present in the body {a=[a], b=[b], c=[c]}

Reference:

What is difference between @RequestBody and @RequestParam?

silentsudo
  • 6,730
  • 6
  • 39
  • 81