简体   繁体   中英

How can i send image/file and nested json data together using postman?

I am able to send image/file and normal key value which is being served as normal json later. I am using the form-data type of body in postman and a node server.

普通键值和图像

To handle image i am using multer on my node server.

But what makes issue is when i try to send the nested json and image together.

I am not able to do this thing.

嵌套的 json 和图像

Everything is fine but this is how the nested json is logging in the terminal:-

在此处输入图像描述

Please. Any help would be great to get the nested data object also in actual json format but not like this string as shown in terminal photo.

JSON can't contain binary data. What you're asking isn't directly possible.

The ideal thing to do is a multipart request, which is what you're getting in your first example. Note that one of those parts could be JSON, and you can just reference the other part by name, or with some other identifier.

The wrong way to do this is to base64 encode the data and put it in your JSON. If you do this, you'll get what you're asking for at the expense of 33% overhead in file size, wasted CPU and memory on each end for encoding/decoding, and significant waste in memory for your JSON parser which now has to chew through all of this extra data.

An alternative is to use a format that supports binary data, like CBOR . CBOR works in browsers , is streamable , supports all of the types of JSON and then some, is extensible , and standardized .

One solution is to split image upload and record upload into two separate services and call in UI one after the other.

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