Home > Net >  Postman: POST request of nested JSON via form-data not working (while via raw-data ok)
Postman: POST request of nested JSON via form-data not working (while via raw-data ok)

Time:09-24

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

CodePudding user response:

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.

CodePudding user response:

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

  • Related