In this tutorial we’ll assume an API server is running on port 3001 on localhost and use Postman to send a POST request to the server’s /user endpoint.
Start up Postman.
Create an Environment
Click the Environments tab on the far left side of the window. Then press the + button to Create new environment.
In the right-side pane change the name of the environment to localhost.
In the table (in the right-side pane) add a variable named url and set it’s Initial value to localhost:3001.
Press Save.
In the upper-right corner of the app, below the Manage accounts icon is a field that reads No environment and provides a drop-down menu. Select the localhost environment you just created.
Create a Collection
Click the Collections tab on the far left side of the window. Then press the + button to Create new collection. When you do so, you’ll be shown a drop-down menu. Select Blank collection.
In the right-side pane you will see the collection is titled New Collection. Rename it to Messages.
Now in the left-side pane you will see your new collection listed. Hover over the Messages bar to reveal a menu of options. Choose the elipsis (…) and select Add folder. In the right-side pane, rename the folder from New Folder to Users.
Create a Request
Hover over the Users folder in the left-side pane to reveal options. Press the + button to Add request.
In the right-side pane, change the name of the request from New Request to New User.
Below the name of the request is a drop-down menu that allows you to choose a method. Choose POST.
To the right of the method set the url to {{url}}/user. If you hover over {{url}} you should see localhost:3001. If not, check that the localhost environment is selected in the upper right corner of the window.
Below the url, select the Body tab. In the drop-down menu below body, select raw. To the right of raw select JSON. You’ve just indicated that the data you send to the API server (in the body of the request) will be in JSON form.
Enter the data that you wish to send to the API server. Since the data is in JSON format the data must be either an object or an array. If the data is an object, all of the property names must be strings surrounded by double quotes. For example:
{
"firstName": "Joe",
"age": 25,
"isAdmin": false,
"scores": [100, 98, 100, 96]
}
Press Save.
Update your API Server
Add an endpoint to your API server’s user router (in src/users/routers/user.js) that sends back the data that is sent to it.
Notice that the method is post. Inside the try-block we retrieve the data from the request’s body property.
router.post('/user', async (req, res) => {
try {
const data = req.body
console.log(data)
res.send(data)
}
catch (error) {
console.log(error)
res.status(500).send(error.message)
}
})
Send a Request
In Postman, press the Send button. You should see at the bottom right a response status code and below that any data that the API server sends back in the response.