Reputation: 4324
Using Spring Boot 2.1.8, I have two methods in a Rest Controller that expect a single file and multiple files respectively. These are the method signatures:
@PreAuthorize("hasAnyRole('ROLE_ADMIN')")
@PostMapping("/uploadMultipleFiles")
public List<FileResponse> uploadMultipleFiles(@RequestParam("files") MultipartFile[] files);
@PreAuthorize("hasAnyRole('ROLE_ADMIN')")
@PostMapping("/upload")
public FileResponse uploadFile(@RequestParam("file") MultipartFile file);
The single upload works just perfect. I am managing to load a single file from a web client, Postman v7.25.0, and from Swagger 2.
But the multiple file method only works when uploading files from Postman, returning a 400 http error code. Therefore, it does not even enter the method.
The error message in both cases (web client or Swagger) is the same:
can't parse JSON. Raw result:
Missing or unreadable multipart file in request
This is the request headers when calling to /uploadMultipleFiles through Swagger (getting error):
This is the request headers when calling /uploadMultipleFiles from Postman (works fine):
This is the request headers when calling /upload from Postman (works fine):
This is the request headers when calling /upload through Swagger (works fine):
Firstly I thought that Content-Type might have something to do with my problem. But Swagger sends always application/json, and it works with the single upload endpoint.
Any idea?
Upvotes: 3
Views: 2920
Reputation: 270
When you are dealing with file uploads using multipartfile, you should set the content type of the request to multipart/form-data
. The screenshot shows that Postman is using multipart/form-data
and not application/json
.
Upvotes: 1