0

Currently, I am developing a microservice that receives an image, height, and width to resize that image in this resolution. I have done all my backend side, now I am struggling on the front side. What I am trying to do is, send an image, height, and width in the same post request from the postman. After searching, I haven't found a way to do it. I can send it through the request param, but I don't think is the correct way, maybe I am wrong. But, thinking that I have to pass whole my object through the request param it sounds ugly.

What I am trying to do is, in postman Body JSON/raw pass values of the three attributes which are multipart file that take the image, height, and width for the resolution. I can successfully pass the width and height, but I don't know how to pass image from the body raw.

Please, can anyone give any idea?

This is my model.

public class TaskDTO {
    private int height;
    private int width;
    private MultipartFile multipartfile;

Endpoint

@PostMapping("/task")
    public void createTask(@RequestBody TaskDTO taskDTO){
        //taskService.createTask(file, width, height);
        //System.out.println(file);
        System.out.println(taskDTO.toString());

        //System.out.println(taskDTO.toString());


    }

Postman Test

enter image description here

Mohamed Mamun
  • 211
  • 3
  • 15

3 Answers3

4

Here is an example using @ModelAttribute:

  • DTO enter image description here

  • Controllerenter image description here

  • Postman UIenter image description here

zzzzz
  • 222
  • 2
  • 7
  • Also this topic could be useful for you: https://localcoder.org/how-to-send-the-multipart-file-and-json-data-to-spring-boot – zzzzz May 17 '22 at 14:30
1

You can send file only using form-data not raw. In this point you will need to search for @RequestParam MultipartFile file.

Another point is to send file as array of bytes. But, you will need to convert your file into bytes before.

zzzzz
  • 222
  • 2
  • 7
  • 1
    thanks for your response. I've tried using the annotation ```@RequestParam```, which only allows sending the file, well also other data. But, what I am trying is, to send the file using ```@RequestParam``` and the JSON data with ```@RequestBody```. – Mohamed Mamun May 17 '22 at 13:48
  • 2
    In this point you should use `@ModelAttribute` and you will be able to send every DTO data as form parameter. – zzzzz May 17 '22 at 13:50
  • Could you attach any examples, please?? I've searched a lot, but I don't found anyone who can give me a correct explanation. – Mohamed Mamun May 17 '22 at 14:04
  • Attached example as a new answer. – zzzzz May 17 '22 at 14:12
0

You can send a multipart message.

See this answer: https://stackoverflow.com/a/8616667/20654

In postman you select

POST

Then form-data and in the key you can select the file

Example

See this for a more detailed answer:

https://stackoverflow.com/a/16022213/20654

OscarRyz
  • 196,001
  • 113
  • 385
  • 569