Reputation: 11930
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)
Upvotes: 0
Views: 5831
Reputation:
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 recommended that you use JSON in your requests and responses. JSON has also somewhat become 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 recommend that you read the following article explaining what JSON is and how you use it: https://www.w3schools.com/js/js_json_intro.asp
Upvotes: 0
Reputation: 96
Your object body is of type text. Change it to JSON using the little dropdown and the POST request will work.
Cheers!
Upvotes: 0
Reputation: 2931
Click the "Text" beside it will show you a dropdown. Just choose "JSON" instead of "Text"
Upvotes: 2
Reputation: 71
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.
Upvotes: 1