Postman what it basically lets you do is generate HTTP requests. So I can create get requests, post requests, delete requests, and see the responses come back from the server. So in our case we’re going to create, get, post, and deletes to our local API. That will essentially let me mimic everything I’m going to do in my app.
How to post new data
We can open multiple tabs in Postman. The first tab I’ll start with just the get, to get all of the to do’s so that we can come back here and check when I’ve added or updated or deleted a to do, that it actually worked. So we have three to-dos they each have an id generated by MongoDB. We’ll use that to try deleting to-dos and updating them. Alright, so let’s go ahead and add a to do. I’ll create a new tab and we’re going to post, create an HTTP request that is of the post type. The post verb allows the proper functions to run inside Node.js. It’ll handle the post and add them to do.
Now the endpoint is sitting at /api/todo. The endpoint will take a couple of different types of verbs. Post and delete. Depending on whether or not we include an id in our body we can both update and add a new one. Postman lets me specify the body. Within the body of Postman with form-data selected it is basically like a query string. But if we had posted a form in our browser then this would be of the same format. A name and value pair. Instead, we’ll select raw and not form-data. Raw data will be raw data, which defaults to text, but we’ll choose JSON since Node.js will be expecting that.
Now I’ll post JSON in the body, which means everything has to be in quotes. What is expected is a to-do property. I’m going to say “todo”: “Buy beef”, “isDone”: “false”, and “hasAttachment”: “false”. So we’re going to add this to-do to our to-do’s. Node.js will handle it, save it to my Mongo database, and that’s it. So let’s post this and hit send. So in my first tab, I should be able to go back to my get and get all of the to-dos in my database. It runs successfully and we have created a new to-do.
How to update data
Instead of posting new data there is a way to update existing data. The only difference is that it will do an update if it finds a property .id inside of JSON. To do this I’ll grab the id. The ids of our to-dos are labeled _id because it is coming out of MongoDB and that is their automatic id. Inside of the body in Postman, we just added new data. However, if we added “id”: “(id of a to-do)” it would update the value of that object associated with that id.
How to delete data
So in Postman I created another tab using the delete verb and the same endpoint /api/todo. Then Node and Express will run that function in response to that. Go to the body in the delete and use raw JSON and remove everything besides the id. Then that indeed removes the to do.