Alwaysblue
Alwaysblue

Reputation: 11930

How to send an object in postman

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

enter image description here

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

Answers (5)

user6001928
user6001928

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

Shraddha Goel
Shraddha Goel

Reputation: 897

Choose the JSON option as shown in the picture.

[1]: https://i.sstatic.net/J9csY.png

Upvotes: 2

mwenda denis
mwenda denis

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

Hải Bùi
Hải Bùi

Reputation: 2931

see image

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

Upvotes: 2

Tomkelele
Tomkelele

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

Related Questions