admin管理员组文章数量:1415475
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 justeval
the string – slebetman Commented Sep 18, 2019 at 7:36
5 Answers
Reset to default 2Click 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
本文标签: javascriptHow to send an object in postmanStack Overflow
版权声明:本文标题:javascript - How to send an object in postman - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745160619a2645424.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论