0

I have a problem to send requestBody and multipartFile in Controller of Spring Boot from Postman.

I get this issue shown below as an image

Image

Here is the method of controller shown below

@PostMapping(produces = MediaType.IMAGE_PNG_VALUE)
public ResponseEntity<byte[]> createQrCode(@RequestBody CreateQrRequest request, @RequestPart MultipartFile imageFile) throws IOException {

}

Here is CreateQrRequest shown below

@Data
@Builder
public class CreateQrRequest {

    @NotBlank
    private String text;

    @NotBlank
    private String size;

    @NotBlank
    private String color;

    @NotBlank
    private String backgroundColor;
}

Here is the error message when I tried to define the parameters like

public ResponseEntity<byte[]> createQrCode(@RequestPart CreateQrRequest request, 
                                               @RequestPart MultipartFile imageFile) throws IOException {

Error Message

{
    "type": "about:blank",
    "title": "Unsupported Media Type",
    "status": 415,
    "detail": "Content-Type 'application/octet-stream' is not supported.",
    "instance": "/api/v1/qr-generator"
}

How can I fix it?

S.N
  • 2,157
  • 3
  • 29
  • 78
  • Best lookup enabling octet-stream in the server config for requests and post. – Samuel Marchant Mar 31 '23 at 23:23
  • @SamuelMarchant How can I enabling octet-stream? – S.N Mar 31 '23 at 23:25
  • What server are you using? Here is some of that type of way. https://stackoverflow.com/questions/75687109/how-can-i-limit-the-body-of-a-application-octet-stream-request-in-spring-boot – Samuel Marchant Mar 31 '23 at 23:26
  • @SamuelMarchant Tomcat. I think there is a problem in createQrCode method. – S.N Mar 31 '23 at 23:29
  • @SamuelMarchant When I use this option `public ResponseEntity createQrCode(@RequestPart String text, @RequestPart String size, @RequestPart String color, @RequestPart String backgroundColor, @RequestPart MultipartFile imageFile) throws IOException` , it works. However, I want to send a RequestBody – S.N Mar 31 '23 at 23:38
  • From both errors it is definitely a server config issue, a QR code however may be a mime type "image/png" or "image/jpg" what does it generate? Too if you are sending in a request from a multiparty form, all you need to do is put "the data" piece through the url encoder first to make it a pseudo string. Post or Get email form "data" is url encoder first! A better way of comprehending it is after all pieces are url encoder first and then joined together finally, it then has a Content-Length – Samuel Marchant Mar 31 '23 at 23:42
  • You then are using multiparty/form-data form type , but is the QR code image an attachment or in the body? If in the body part then it is best type text/html and in an html IMG element to use a base64 image data coding in the SRC= – Samuel Marchant Mar 31 '23 at 23:46
  • @SamuelMarchant I don't think there is a server issue. The problem is related with this part `(@RequestPart CreateQrRequest request, @RequestPart MultipartFile imageFile) ` – S.N Mar 31 '23 at 23:54
  • When using application/octet-stream the content-transfer-encoding would be "binary". However I presume much of this is done by the system. The answer by someone below seems credible from this tutorial. https://www.baeldung.com/java-generating-barcodes-qr-codes – Samuel Marchant Apr 01 '23 at 00:07
  • @SamuelMarchant I use ZXing library in my example. My issue is related with the request part with variables. `@RequestPart` didn't help me fix the issue. – S.N Apr 01 '23 at 00:28
  • Not finding good api info for it , this following is in some way a Result not a requestpart @RequestPart MultipartFile. Too, is the byte[] array returned only image data or a whole message? Is that emailed or sent straight out http "Are you writing your own annotations for this code?". E.g. Qrproductioninput Qrproductioninputparam Qrproductionoutput – Samuel Marchant Apr 01 '23 at 03:01

1 Answers1

1

You can change the controller like below -

 @PostMapping(produces = MediaType.IMAGE_PNG_VALUE)
    public ResponseEntity<byte[]> createQrCode(@RequestPart String text, @RequestPart String size, @RequestPart String color, @RequestPart String backgroundColor, @RequestPart MultipartFile imageFile) throws IOException {

    }

Just writing the request's attributes separately.

  • Is it impossible to use CreateQrRequest request? – S.N Mar 31 '23 at 23:17
  • Not sure, once I faced this issue, and solved it this way. If you find anything, please post the solution here. – Md. Nowshad Hasan Mar 31 '23 at 23:21
  • I have a problem about the custom validation in this example. Can you help me if you have any idea. Here is the link : https://stackoverflow.com/questions/75918125/how-to-use-custom-validation-with-requestparam-in-spring-boot – S.N Apr 03 '23 at 10:46