简体   繁体   中英

How to Send file and data via raw JSON from Postman

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

在此处输入图像描述

Here is an example using @ModelAttribute :

  • DTO在此处输入图像描述

  • Controller 在此处输入图像描述

  • Postman UI 在此处输入图像描述

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.

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

例子

See this for a more detailed answer:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM