#Spring boot post request send multiple files with keys

1 messages · Page 1 of 1 (latest)

median hazel
#

Hello, I would like to send files to rest api like

 {
  photo : file
  driver_licence : file 
}

I don't want to use request param because I will send more than 5 file
Is is possible in spring

velvet galeBOT
#

<@&1004656351647117403> please have a look, thanks.

velvet galeBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

Yes, it is possible to send multiple files with keys in a POST request using Spring Boot. You can achieve this by using the @RequestParam annotation in your controller method.

Here's an example of how you can do it:

@PostMapping("/upload")
public String uploadFiles(@RequestParam("photo") MultipartFile photo,
                          @RequestParam("driver_licence") MultipartFile driverLicence) {
    // Process the files here
    // ...
    return "Files uploaded successfully";
}

In this example, the @RequestParam annotation is used to bind each file to a specific parameter. The parameter names (photo and driver_licence) should match the keys used in your JSON request.

You can also use a List<MultipartFile> if you want to send multiple files with the same key:

@PostMapping("/upload")
public String uploadFiles(@RequestParam("photos") List<MultipartFile> photos) {
    // Process the files here
    // ...
    return "Files uploaded successfully";
}

In this case, you would send an array of files with the key "photos" in your JSON request.

Remember to include the necessary dependencies in your project's build file (e.g., Maven or Gradle) for handling multipart file uploads.

brazen niche
#

Hello! I don't know if this answers your questions or not but you can use the ModelAttribute annotation to pass in a Pojo that is representitive of your file structure. Maybe that would make it easier? I did this recently to organize all of my expected request params into an object that's referencable and makes it more OO like. https://www.baeldung.com/spring-mvc-and-the-modelattribute-annotation --> this will probably explain it better.

Baeldung

A quick, practical writeup about the @ModelAttribute annotation in Spring MVC.