最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to send an object in postman - Stack Overflow

programmeradmin0浏览0评论

I was trying to make a post request to the api route I just created.

In the backend I have something like this

console.log(typeof req.body)
console.log(req.body)
const { firstName, lastName, email, phoneNumber } = req.body
console.log(`Variable Values`, firstName, lastName, email, phoneNumber)

Here I am getting typeof as String and body as this

{
        firstName: "Varun",
        lastName: "Bindal",
        email: "[email protected]",
        phoneNumber: "+91-8888"
}

What I want is that the typeof to be object so I can de-structure it, How can I make a request from postman in this case (I don't want use JSON.parse)

I was trying to make a post request to the api route I just created.

In the backend I have something like this

console.log(typeof req.body)
console.log(req.body)
const { firstName, lastName, email, phoneNumber } = req.body
console.log(`Variable Values`, firstName, lastName, email, phoneNumber)

Here I am getting typeof as String and body as this

{
        firstName: "Varun",
        lastName: "Bindal",
        email: "[email protected]",
        phoneNumber: "+91-8888"
}

What I want is that the typeof to be object so I can de-structure it, How can I make a request from postman in this case (I don't want use JSON.parse)

Share Improve this question asked Sep 18, 2019 at 7:08 AlwaysblueAlwaysblue 12k44 gold badges141 silver badges253 bronze badges 2
  • You will have to parse it somehow. Parsing javascript object syntax is fairly plicated if you want to support all possible features (expressions, puted property, ments etc.) which is why the JSON format was designed specifically simple that it is still patible with javascript object syntax but also easier to implement a parser for – slebetman Commented Sep 18, 2019 at 7:34
  • HTTP will only ever send strings to the backend (even JPEGS are simply binary strings) - if you don't want to use JSON you need to implement a parser manually. In the old days before JSON.parse was implemented a simple cheat is to just eval the string – slebetman Commented Sep 18, 2019 at 7:36
Add a ment  | 

5 Answers 5

Reset to default 2

Click the "Text" beside it will show you a dropdown. Just choose "JSON" instead of "Text"

Choose the JSON option as shown in the picture.

You should change the type of body from raw text to JSON (application/json) by clicking on the text button right next to your GraphQL option.

Your object body is of type text. Change it to JSON using the little dropdown and the POST request will work.

Cheers!

Why don't you want to use JSON.parse?

It's important to know that JSON and a javascript object are two different things.

JSON is a data format format that can be used in various environments while a javascript object is a data structure/concept in javascript.

When making a HTTP request you can send data via a few different methods. A few prominent ones being XML, Binary and JSON (They all will be represented as text, even binary).

Since you're building a API with javascript I would remended that you use JSON in your requests and responses. JSON has also somewhat bee the "standard" for APIs these days. It's also very easy to parse JSON to javascript objects and the other way around.

Please note that you maybe also need to tell postman to set the Content Type Header to application/json. You also would need to change your body to be actual valid JSON:

{
  "firstName": "Varun",
  "lastName": "Bindal",
  "email": "[email protected]",
  "phoneNumber": "+91-8888"
}

I can remend that you read the following article explaining what JSON is and how you use it: https://www.w3schools./js/js_json_intro.asp

发布评论

评论列表(0)

  1. 暂无评论