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:
- Send each object as a
Blob
file with anapplication/json
content type as suggested here - Send each object as a
String
as suggested here - Add
contentType: false
andprocessData: false
in the ajax request as suggested here - 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");
}