'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 enter image description here



Solution 1:[1]

I managed to make it work! (note: I added some additional fields compared to the screenshot in question. See below for details:

Postman

Solution 2:[2]

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.

Solution 3:[3]

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

Solution 4:[4]

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"
    ]
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Daniel
Solution 2 lucas-nguyen-17
Solution 3 PDHide
Solution 4 Tyler2P