Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
by
Lessons:2Length:10 minutes
  • Overview
  • Transcript

1.2 Testing REST APIs With Postman

In this lesson, I’ll show you how to use Postman to issue HTTP requests to our REST API, save all of these requests into a collection, run tests against the results, and automate the execution of these tests to quickly see if everything in the API is working correctly. All that in less than 10 minutes!

Related Links

1.Testing REST APIs With Postman
2 lessons, 09:32

Free Lesson
1.1
Introduction
01:34

1.2
Testing REST APIs With Postman
07:58


1.2 Testing REST APIs With Postman

[MUSIC] Let's begin by diving right into Postman. Now typically when you are using a tool like this, what you're trying to do is verify the functionality found within some sort of web service. And typically when I use it, I'm trying to test out a RESTful API that is going to be speaking back and forth using JSON formats. Now in this particular case, that is my example where I'm going to have a todo RESTful API but in the background that can right now return back a list of todos, return an individual todo, and also create new todos within the system. So if I were to open up Postman and by default you're gonna be in the builder interface here. And if I were to create a new tab at the top just by hitting the plus button and pasting in my URL and saying I want to create a GET request, this is going to give me back a list of the configured todos. So if I were to hit Send, I would get back a response of the initially configured todos within the system and there are three right now, wake up, wash face, eat breakfast. And, as you can see, two of the three are currently done. I also got back a status code of 200, and that's gonna be very important, and I'll show you why in just a moment. Well let's say I only wanted to get this second todo within the list. Well I could create another request, and I could put the ID on the end, and that's the way that I have the current RESTful API set up. I could hit Send and I'm gonna get back just the second one, which is wash face which is what I would expect. And then finally, let's say that I wanted to attempt to create a todo, well I know I would have to issue a POST request to that particular URL. And in this case I would have to send a body, but I'm gonna leave it blank for now, just to give a little bit of an example here. And as you can see, it's getting a 400 bad request, to say that the name field is required. Okay, this is very interesting. So what I wanna do is I want to take this very manual process of me issuing these requests, and then being able to do them in a much more automated fashion so I can make sure things are happening correctly. Well let's start by verifying the information that's coming back. So I could have a user send these requests and verify this information but wouldn't it be nice if I could, in a little bit more of an automated fashion, do exactly that. Well, I can absolutely do that. So I'm gonna stay within this builder tab here and what I wanna do is go underneath the request, go over to the right and select Tests. Now in here you're able to write a little bit of script to verify the information that comes back. So I'm going to show you a couple easy ways to get started with this. If I come over to the right into the snippets grouping here and scroll down just a little bit, all I really want to do is verify that maybe the status code for this request is coming back correctly. That the status code was actually 200. So I can come down here and select Status code: Code is 200, and it's gonna drop a little bit of script into my code. So as you can see here, this says Status code is 200. This is gonna be the name of the test that's gonna show up once it executes. And the result of that, whether it's a pass or fail is dictated by the response Code.codefield that comes back which corresponds to the value of the ID for the status code that comes back, but we wanna verify that it's a 200. Well, let's go ahead and hit Send. And as you can see here, I get my individual response back, but I also get test results back, whereas you can see, I have a pass here for the Status code is 200. And once again the Status code is 200 comes from the text put right in here within quotes. So now I can see very visually that, okay, my test passed. This looks like the request is working correctly. All right, let's do the same thing for the second request but let's add a little bit more functionality into these tests. All right, so let's go back over to the test, I'm still wanting to know if this, return to 200 correctly, so let's leave that there. But let's also verify some of the information that's coming back. So if I scroll up a little bit, you're gonna see here that I can verify the response body and then do a JSON value check. So what's gonna happen here is it's gonna get the response body, it's gonna parse that as JSON, and then it's going to verify some information. Well let's say I wanna make sure that that ID that came back was actually 2 because that's the one that I was requesting to get back and we're just gonna call this Id is 2. So that you can see this change, and then I also want to do another test here. I wanna verify the name, so I want to say name is wash face. So this time, I want to make sure that the name property that comes back is actually equal to wash face. So, now I can issue my request, and I can see that the response here I get back all of my data, and that all three of my tests passed. The ID is 2, the name is wash face and the status code is 200. All right, we'll, let’s see what we can do over here on this side as far as this todo for creation. Now in this case, I do want to be testing bad requests and this is truly a bad request because the name field is required. So just very quickly, let's jump over into Tests here. Let's do a simple verify of our status code, and I want to make sure that this winds up being a 400. Now you can also make sure, or validate the information that's coming back to make sure it's the correct missing value. And you can absolutely add that in there if you would like, but I'm gonna go ahead and hit Send. And while I got a 400 bad request, my test did pass because that's what I was expecting although I need to change this to be 400, so that it makes more sense. So status code is 400. So now I've got these three tabs and this is great but this will become a very big nightmare to maintain if I have to go and continually hit Send all the time to make sure all these things are working. Well, with Postman, there's a much more efficient way to do that. Let's start by saving these, but I wanna save them in a special way and that special way is into a collection. So I wanna come over into Collections here and I'm gonna hit this plus button here. This is gonna create a new collection and I'm gonna call this Todo. So I'm gonna create a new collection here as I've gotten here. Now I wanna save these, I wanna save these into my new Todo collection. So I wanna call this Get all, and I wanna put this into Todo, I'll save that. I want to save this one here and I'm gonna save this one as Get 2, and I want to save this into Todo as well. And the last one, I want to also save this one, but let's just say we want to call this missing Name POST. Like that, and I wanna save this into Todo as well, so, I'm gonna save that. Now, I'm gonna close all of these. So, as you can see now within my collection of Todo here, I have three individual requests here, all with the names that I save them as. Get all, Get2, as well as Missing Name POST. So what I can do now, once I've created the several collections, and you can organize these in any way that you want. You can organize them by functionality, by resource within your API, whatever you'd really like to do. But once I've done that, I can come into my collections and I can click on this little right arrow here, and I can select Run. Now this is going to open up the Postman test runner so I could select one of my collections here, in this case I'm gonna make sure that Todo is selected, and then I'm gonna come down here and run Start Test. Now this is gonna issue all of the requests that are saved within that collection. It's going to show the name, the URL that it went to, the response that came back as well as any of the tests that were associated with that particular request. So as you can see now, I can start to build up a very large collection, or library of collections, to be able to run automated tests against my RESTful API, without having to go in there and manually verify the results every time. So hopefully this little tip will allow you to decrease the amount of time it takes for you to run your tests against your RESTful APIs. So I hope that's helped and I'll catch you next time.

Back to the top