[英]Postman: POST request of nested JSON via form-data not working (while via raw-data ok)
I want to POST the following JSON-object via Postman:我想通过 Postman 发布以下 JSON 对象:
{
"title": "test_title",
"date": "2021-12-31",
"attachments": [
{
"name": "test_attachment"
}
]
}
This works perfectly fine, when using Postman's raw input form for the request-body: I get a "201 Created"-response back.当使用 Postman 的原始输入表单作为请求正文时,这非常有效:我得到了“201 Created”响应。
However, when using the form-data to POST the data, I get the error "Invalid data. Expected a dictionary, but got str."
但是,当使用表单数据 POST 数据时,我收到错误
"Invalid data. Expected a dictionary, but got str."
(see also screenshot below) What am I doing wrong here? (另见下面的截图)我在这里做错了什么? I tried all kind of other versions to enter the attachment-key:value pair but nothing worked so far
我尝试了所有其他版本来输入附件键:值对,但到目前为止没有任何效果
You did nothing wrong.你没有做错什么。
raw type
(json) in postman.raw type
(json)。form-data
form-data
var express = require('express')
const multer = require('multer')
const upload = multer()
var app = express()
app.use(express.json());
app.post('/test',upload.none(), function (req, res, next) {
res.send(req.body)
})
app.listen(80, function () {
console.log('web server listening on port 80')
})
Above is a sample endpoint which works with both form-data and json , just do a post to http://localhost:80/test with both form data and raw json上面是一个示例端点,它适用于 form-data 和 json ,只需使用表单数据和原始 json 发布到 http://localhost:80/test
you can see both will get parsed correclty你可以看到两者都会被正确解析
APIs are just abstraction , its like a function that takes in many attribute, how you parse it depends on the implementation ( how the api function is written) . API只是抽象,它就像一个包含许多属性的函数,你如何解析它取决于实现(api函数是如何编写的)。
so answer is "Talk to the developer" on how the API is implemented and what it is supporting所以答案是“与开发人员交谈”,了解 API 的实现方式及其支持的内容
I'm having issue in placing json into form format the way Daniel did in Postman.我在将 json 放入表单格式时遇到问题,就像 Daniel 在 Postman 中所做的那样。 Need help in figuring out what is it required to place the cascaded json objects into form data format.
需要帮助弄清楚将级联 json 对象放入表单数据格式需要什么。 Please see here that I'm trying to accomplish.
请在此处查看我正在尝试完成的工作。
JSON Format (to be filled into Postman form-data section: JSON 格式(填写到 Postman 表单数据部分:
{
"primary_object": {
"child_object_1": [{"id": 12345678, "value": "abc"},{"id": 87654321, "value": "xyz"}],
"child_object_2": [
"first_val",
"second_val"
]
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.