2

I am trying to send a file and two json objects to my Spring Boot backend with a multipart POST request but I always get a 415 http response. Here is a list of things that I already tried to do:

  1. Send each object as a Blob file with an application/json content type as suggested here
  2. Send each object as a String as suggested here
  3. Add contentType: false and processData: false in the ajax request as suggested here
  4. Use @RequestParam instead of @RequestPart in Spring Boot controller

What am I missing?

Here is the request:

const data = new FormData();
data.append('file', new Blob([file], {type: 'multipart/form-data'}));
data.append('entity1-info', new Blob([JSON.stringify(entity1Object)], {type: 'application/json'}));
data.append('entity2-info', new Blob([JSON.stringify(entity2Object)], {type: 'application/json'}));

return axios({
   method: 'post',
   url: url,
   headers: {'Authorization': `Bearer ${idToken}`},
   data: data,
   contentType: false,
   processData: false
});

And here is my controller in Spring Boot:

@PostMapping("/upload")
    public ResponseEntity<String> uploadFile(@NotEmpty @RequestPart("file") MultipartFile multipartFile, @NotNull @RequestPart("entity1-info") Entity1 entity1, @NotNull @RequestPart("entity2-info") Entity2 entity2, HttpServletRequest request) {
        log.debug(request);
        ...
        return ResponseEntity.ok("ok");
    }
davnig
  • 332
  • 5
  • 16

2 Answers2

0

You have to set the "Content-Type" to the header

Content-Type:multipart/form-data

I'm using similar to the below curl command and it's working fine.

curl -v -H "Content-Type:multipart/form-data" -F "entiry1-info=@person1;type=application/json" -F "entiry2-info=@person2;type=application/json" -F "file=@logo.png;type=multipart/form-data" -X POST http://<IP_Address>:8080/api/upload

Note: above person1 and person2 are json files.

darshakat
  • 679
  • 3
  • 15
  • @H.Saul As I mentioned above, did you try curl command to your url with "-v -H "Content-Type:multipart/form-data"" ? – darshakat Jan 06 '21 at 16:30
  • Yes, I tried that. But the problem was in the url itself as I specified in my answer. – davnig Jan 06 '21 at 16:41
  • Then how did you say that "That doesn't work neither"? if you call the correct url, definitely, above curl command working without any issue. if you call the wrong url, It's your mistake, it doesn't mean that my answer doesn't work. – darshakat Jan 06 '21 at 16:47
  • You suggested me to try to set the Content-Type in the header. That's what I've done. And that didn't work. If I'd had the correct url from the beginning then I wouldn't have asked the question. – davnig Jan 07 '21 at 09:17
0

I eventually found the reason why I was always getting 415 http response from my Spring Boot backend.

The problem was that the url of the request was wrong. It was pointing to an endpoint that was expecting only json data and not multipart. And that's why Spring Boot was returning Unsupported media type.

Hope this could help someone else.

davnig
  • 332
  • 5
  • 16
  • Can you share you js (axios) and java server code (/upload function). I cannot get it working properly. Thanks – jvence Jan 09 '22 at 11:51