I am currently doing an experiment to build the API which users can submit freely at their own will without restriction, no boundary on field names and the contents as well. And the API would just print out the inputs in a key,value pairs. For the files, I would just print out the whole bytes of the base64 converted of it.
For example, I have the request A like this:
{
"name": "John Doe",
"lucky_number": 75
}
Then the API will get the output:
Hello user, your first key is name and its value is John Doe, your next key is lucky_number and its value is 75. Thank you for using.
Then, I make another request with the fields below. The image actually was filled with actual image/jpg file.
"pokemon": "pikachu",
"image": <File>JFIFw9d1j3f0881ehfce0cn2e0f2
So, it will print out most likely:
Hello user, your first key is pokemon and its value is pikachi, your next key is image and its value is cApovnquGCaisa1b23IAbqxcaiFoPqR==. Thank you for using..
Can this actually be achieved? As for what I have known, I can make such dynamic request if the request body was formatted in JSON file that Spring can easily get it to the Java Object format. But what about this multipart? And as far as I know, I can get the multipart only with the defined fieldname, and this code below most likely can only handle one file with field file, but what if the user actually send 3 files or even more as there's no boundary as the expected behavior?
@PostMapping("/fun", consumes = { "multipart/mixed", "multipart/form-data" }, produces = {"text/plain"}
public String postForFun(@RequestPart("file") MultipartFile file, @RequestBody Object dynamicFields) {
}
CodePudding user response:
