How To Test APIs With Postman

How can we actually test our API that we build the previous blog? Today we’re going to use a utility called Postman to test our API. It is clean and easy to use.

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.

Calling the API via browser JavaScript. Via the client side to the server and in our case, via AngularJS. In Postman I create my request and I’m going to create a get request. I’ll say localhost:3000/api/todos/test. Postman will give us the status, how long it took, and the actual results. I also see my JSON results and I can see the HTTP headers that came back. So now I’ve tested the API on the browser. However, post and delete will be harder.

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.

A summary of what actually happened to post a new to-do. I sent an HTTP request with the verb post. Node.js handled it. I sent it to localhost:3000 so that was the port. Node.js was the process on the computer that ended up handling, then, that request. And after we post it, Express took that HTTP request, Node and then Express helped it in order to be able to say, “well now this is the function that should run”. In response to the URL that was requested, and that’s part of the HTTP request, what URL it was. Then it looked at the body and took this JSON and turned this into a JavaScript object that was made available via request(req.body). So this thing became an object, in JavaScript .body on the request object.

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

The last thing we haven’t tried is deleting a to do. So let’s delete a to do, a side note is that all it expects in the body is a JavaScript object, created from JSON, that has the id in it. Nothing else. It just needs to know the id of the to do that we want to delete.

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.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Up ↑

%d bloggers like this: