简体   繁体   English

邮递员:通过表单数据的嵌套 JSON 的 POST 请求不起作用(而通过原始数据可以)

[英]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我尝试了所有其他版本来输入附件键:值对,但到目前为止没有任何效果在此处输入图像描述

I managed to make it work!我设法使它工作! (note: I added some additional fields compared to the screenshot in question. See below for details: (注意:与有问题的屏幕截图相比,我添加了一些额外的字段。有关详细信息,请参见下文:

在此处输入图像描述

You did nothing wrong.你没有做错什么。

  • If you want to make a request with json object, then you go with raw type (json) in postman.如果您想使用 json 对象发出请求,那么您可以在 postman 中使用raw type (json)。
  • If you want to upload file, then you use form-data如果要上传文件,则使用form-data
  • One more thing, status 201 means the request is succeed, your object has been created.还有一件事,状态 201 表示请求成功,您的对象已创建。
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.

相关问题 邮递员原始数据有效,但表单数据不适用于节点中的 POST 请求 - Postman raw data works but form-data not works on POST request in node 如何将 Postman 正文中带有嵌套数组字段的原始 JSON 转换为表单数据? - How to convert raw JSON with nested array field in Postman body into form-data? 使用 Postman 通过原始 JSON 发送 POST 数据 - Send POST data via raw JSONwith Postman 如何在邮递员的同一请求中发送多部分/表单数据和嵌套的json? - How to send multipart/form-data and nested json in the same request in postman? 邮递员在尝试使用表单数据执行 POST 时返回 415,但与 JSON 相同的请求工作正常 - Postman returns 415 when trying to do a POST using form-data, but the same request with JSON works fine 如何通过邮递员在POST请求中发送嵌套的json - How to send the nested json in POST Request via postman 通过HTTPS POST将JSON,多部分/表单数据发送到url(C#) - Send JSON, multipart/form-data via HTTPS POST to url (C#) 邮递员表单数据和 x-www-form-urlencoded 工作但原始 json 没有 - Postman form-data and x-www-form-urlencoded work but raw json doesn't 我可以通过原始 JSON 和 Postman 发送 GET 数据吗? - Can I send GET data via raw JSON with Postman? 使用 axios 在 POST 多部分/表单数据请求中发送文件和 json - sending file and json in POST multipart/form-data request with axios
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM