0

I want to upload my image together with some boolean flag in my post request. I want these two values to be in the body so I used multipart/form-data content type, and want to do something following:

void uploadNewImage(
            @RequestPart Boolean flag,
            @RequestPart("file") @NotBlank MultipartFile file) throws IOException {
        ....;
}

but when I use swagger, I get an exception: "message": "Content type 'application/octet-stream' not supported"

I googled for some advice and all of them to change @RequestPart -> @RequestParam. It works indeed, but in the request that is sent by swagger, I see flag passed as the query param, but not in the body.

So I'm wondering how can I pass flag in the body

Update 1:

If my flag is in the body anyway since it's POST, why people use multipart/form-data in such cases, when they want to upload an image, and transfer a simple java type (String/Long) Can I proceed with a simple code like the above? Does it mean the same?

void uploadNewImage(
            @RequestParam Boolean flag,
            @RequestBody byte[] image) {
        ....
    }

Of cause omit that in one case I use MultipartFile and in another - byte[], but rather than this - there is no difference, and flag will be passed in a query(again, is it gonna be in the body here as well?)

a3dsfcv
  • 1,146
  • 2
  • 20
  • 35
  • in a GET request, query params are the body since a GET request does not have a body. therefore, in a POST request they are part of the body too although they are contained in the url. What do you mean when you want flag in the body. – midugh Feb 03 '21 at 21:24
  • @R.LM can you check my `update 1` please – a3dsfcv Feb 03 '21 at 21:42
  • instead of an octet stream, you are here instantiating a byte array which does not seem to be the best option since the image can be very fat and then cause the app to be slow or event in some extreme cases. take a look at this answer https://stackoverflow.com/a/49991403/10707187 it shall help you out – midugh Feb 04 '21 at 09:35
  • Yes, but I'm asking not about the benefit of `MultipartFile`, but about the benefit of `multipart/form-data / @RequestPart("file")` over the trivial `@RequestBody` – a3dsfcv Feb 05 '21 at 07:37

0 Answers0