简体   繁体   中英

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:

{
    "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.

However, when using the form-data to POST the data, I get the error "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.
  • If you want to upload file, then you use form-data
  • One more thing, status 201 means the request is succeed, your object has been created.
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

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) .

so answer is "Talk to the developer" on how the API is implemented and what it is supporting

I'm having issue in placing json into form format the way Daniel did in Postman. Need help in figuring out what is it required to place the cascaded json objects into form data format. Please see here that I'm trying to accomplish.

JSON Format (to be filled into Postman form-data section:

{
  "primary_object": {
    "child_object_1": [{"id": 12345678, "value": "abc"},{"id": 87654321, "value": "xyz"}],
    "child_object_2": [
      "first_val",
      "second_val"
    ]
  }
}

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