Home > Enterprise >  How to Send file and data via raw JSON from Postman
How to Send file and data via raw JSON from Postman

Time:05-17

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

CodePudding user response:

Here is an example using @ModelAttribute:

  • DTO enter image description here

  • Controllerenter image description here

  • Postman UIenter image description here

CodePudding user response:

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.

CodePudding user response:

You can send a multipart message.

See this answer: Example

See this for a more detailed answer:

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

  • Related