简体   繁体   English

如何使用 postman 一起发送图像/文件和嵌套的 json 数据?

[英]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.我能够发送图像/文件和正常的密钥值,这些值在以后作为正常的 json 提供。 I am using the form-data type of body in postman and a node server.我在 postman 和节点服务器中使用表单数据类型的正文。

普通键值和图像

To handle image i am using multer on my node server.为了处理图像,我在我的节点服务器上使用 multer。

But what makes issue is when i try to send the nested json and image together.但问题是当我尝试将嵌套的 json 和图像一起发送时。

I am not able to do this thing.我不能做这件事。

嵌套的 json 和图像

Everything is fine but this is how the nested json is logging in the terminal:-一切都很好,但这就是嵌套的 json 登录终端的方式:-

在此处输入图像描述

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.任何帮助都可以很好地获得嵌套数据 object 也是实际 json 格式,但不像终端照片中显示的这个字符串。

JSON can't contain binary data. JSON 不能包含二进制数据。 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.请注意,其中一个部分可能是 JSON,您可以仅通过名称或其他标识符引用其他部分。

The wrong way to do this is to base64 encode the data and put it in your JSON.错误的方法是对 base64 编码数据并将其放入 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.如果你这样做,你会得到你想要的,代价是文件大小的 33% 开销,浪费 CPU 和 memory 在每一端进行编码/解码,以及 memory 中的大量浪费 Z0ECD11Z18ser23 现在有仔细研究所有这些额外的数据。

An alternative is to use a format that supports binary data, like CBOR .另一种方法是使用支持二进制数据的格式,例如CBOR CBOR works in browsers , is streamable , supports all of the types of JSON and then some, is extensible , and standardized . CBOR在浏览器中工作,可流式传输,支持JSON 的所有类型,然后是一些,可扩展标准化

One solution is to split image upload and record upload into two separate services and call in UI one after the other.一种解决方案是将图像上传和记录上传拆分为两个单独的服务,并在 UI 中一个接一个地调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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