Using Postman | Bondar Academy
Course: Mastering testRigor
Module: Working with APIs
Instructor: Artem Bondar
Lesson Summary
This lesson covers how to use the Postman application for performing API calls . Postman is a user-friendly tool that helps in debugging API requests and preparing API mocks before implementation. Getting Started with Postman Download Postman from postman.com for Windows, Mac, or Linux. Create a free account and log in to access the application. Performing API Calls To make an API call: Open Postman and click on the plus tab to create a new request. Select the request type (GET, POST, etc.) from the dropdown. Enter the API endpoint URL and click Send . Example API Calls GET Request: Retrieve a list of tags or articles by entering the appropriate endpoint. POST Request: For signing in, provide the URL, body payload (username and password), and click Send . Handle responses, such as status codes 200 for success or 403 for forbidden access. Creating and Deleting Articles To create a new article: Manually create an article in the application to observe the payload. Simulate the creation in Postman by using the same endpoint and payload. Ensure to include the Authorization header for secured endpoints. For deleting an article, use the DELETE request with the article's slug ID and include the authorization header. Conclusion Postman is an effective tool for testing and debugging API requests. It allows users to save requests and practice before implementing them in scripts. Always ensure to provide the correct request type, URL, and authorization headers for secure connections.
Video Transcript
In this lesson, I will show you how to use Postman application to perform API calls. So Postman is very convenient application to debug your API requests, to play around with the APIs, because it has a very nice user interface, and you have a clear understanding what you are doing. So using Postman is very helpful, for example, to prepare your API mocks, or prepare your API requests before implementing this in the test trigger. And in this lesson, I will show you how to use Postman, how to perform different API calls, and later, we will perform those API calls in the test trigger. So let's get into it. So first of all, you need to download the Postman application. So go to the postman.com and download this app. You can have a different options for Windows, for Mac, or for Linux. Just download the app. I will not show you how to do it. It's just straightforward process. Download the binary, install. You will need to create a free account in Postman app, and login. After you do this, you open Postman application on your site. It looks like this, and you can work right away. So to perform API calls, again, let's start with something simple. So you click on this plus tab, and you have this nice interface. Here in the dropdown, you can select what type of the request you want to send. This is the field for the URL send button, and then you will see the response provided. So let's practice a little bit with something what we have so far. For example, let's try to get the list of tags that we already did before. So this is our API endpoint. I copy this API, paste it right here. This is the list of tags endpoint. This is a get type of call. Okay, you need to make sure that you don't have any special characters as part of your string, get, and just click Send. Here we go. The request was successful. You can see status code 200, the time 220 millisecond, the size of the response object, and this is the object that was returned, the list of tags. So let's try the second one. I copy this one, going back to Postman, and I create a new call, for example, get list of the articles. Okay, and look, this particular get endpoint has two parameters, limit of 10 and offset zero. And Postman automatically extract those parameters from the URL and put it right here in this nice table. So I click Send, and also 200 status code, and the list of articles is provided, and it is in the JSON format. If you want, you can transform it to text, HTML, or something, but JSON is something that's convenient to use. Also, you can expand or collapse, review the individual objects in this window, which is also very convenient. And let's say if you want to get five articles, you just update this parameter, get five, click Send, and now the response is just five articles in this list. And let's say one article, will it work? Yeah, and just one article is received. So this is how it works. And this is a pretty simple case, right, get request. But how about other types of the request? For example, going back to application, and I perform a sign in. Test rigor user at test.com. Welcome, sign in. Here we go, sign in was performed. Now let's see what happened here. So we have a call to login endpoint, this guy. This is a post request 200, and also this request had a payload. So you can click on View Source, and this is the payload that was performed. So let's simulate this request as well. So the response is this, response provide the user access token as a result. Let's simulate this post request. So I copy this login, create a new request. This is a post type of the request, paste this URL, and I need also a body. So payload, I copy this, this is my username and password. Going back, and here is a body tab. I click on the body, then I click on Raw, and the format is JSON. Paste it right here, and I click the button Beautify. So it will be nicely presented in a kind of JSON object, vertically aligned and formatted, and it will be easier to use. That's it, now I send post request. The response was 200, and we have a successful response. Let's say if I provide invalid username or password. Let's say I send welcome123, which is invalid password for this account. I send it one more time, and here we go. We have 403 status code, which is forbidden, and error message. Error, email or password is invalid. So going one more time, welcome, send it again, and this request was valid. We have a user access token returned over here. So moving on, so let's say that we want to create a new article using API. First of all, let's create it manually, and we will see what application is doing in the networking tab. And then we will simulate the same exact process using the Postman. So I click New Article and create, for example, this is a test article. Short description, this is a sample body. Something like this, and I click Publish the article. All right, article is published. Let's see in the response tab. So this is the article's endpoint. We have API articles. This is a post status call, and the payload was this one. So if I review this, you can see this is exactly the payload that we have created. So all I need to do is just copy this information from the networking tab into the Postman to simulate the article's creation process. So going back, I need the URL, and I create a new request. Post request, this is my URL. Then I need body, payload is this one. And body raw, copy it here, and I click Beautify. This is my request. And yeah, I can click Send, and I'm sending the request and error. Now we have 401, status error, unauthorized, because we did not provide authorization header. We can create articles on this application only when we are authorized as a user. So we need to provide authorization. If we look into this headers, scroll a little bit down, and here we go. This is authorization header, and this is how this header looks like. This is our header value, authorization header value. So we need to add this authorization header into the list of header in Postman as well, so our request will be authorized. Let's go back. So I click on the Headers tab right here. In the key, I type authorization. And in the value, I need to provide the value which we see here inside of the browser. So I'm creating a new title of the browser and providing the value. Here we go, and token value is based. Let's send it one more time. Send it again, and now we have another error, 422, error title must be unique. So our application does not allow creating a duplicated articles with the same title. So every time you create a new title, this title have to be unique. So let's go back and update the article, the title. For example, this is the test article two, something like this. And I send one more time, and now it was successful. Now I have status 201 created, and this is the article that we have created using API. Now let's go back to application and look what we see in the main tab. So this is the test article that we created initially. And this is a test article that we created using API call without using UI whatsoever, to just API using Postman. And now let's, for example, delete this article. So how it's going to happen? So let's see, I clear this, and let's see what happened in the networking tab when I click a delete article button. And look at this. So we can see application make delete request to this endpoint. And look at this. This is a test article with some weird number. So this URL is a slug ID. Where can you get this slug ID? Slug ID you can find in the response for the article. So let's go to the Postman and find the article that we created before, this one. So in the response, we have this response slug, and this is the ID of this article. So if we make the API request, delete API request with this slug ID, we will be able to successfully delete the article. And our call also have to be authorized. So let's simulate this scenario. So I copy this URL, creating a new request. This will be a delete request. Copying is here, but we need to replace the slug ID with the article that we want to delete. So this is the article that we have created. Okay, this is the test article two. We also need to provide authorization header. Same for the post request, because if you are not authorized, you cannot create article that you not created, right? So going to authorization, let me copy it from here. Copying token value, pasting token over here. And yeah, let's send it. Okay, and status 204, it means that request was successful. And I just simply refresh the page. And here we go, article disappeared from the page. So the article was deleted successfully. So this is how you work with Postman when you work with the APIs. And very easy and convenient tool. So we created different types of APIs. You can switch between the tabs over here. You can save those requests into your collections to use later on, you know, for your later reference. And before trying to implement any of the requests inside of the test trigger, it's very useful to play around first with APIs, practicing API requests, processing the response to see if your request is correct or not. To debug your request first before transitioning this into your script in the test trigger. You will simplify your life a lot if you follow that way. So let's make a quick summary. In the Postman application, you can perform different API calls. All you need to provide is the type of API request that URL and if it is a post or put API calls, you need to provide a request body in the body tab. So you can use a raw button, this raw button, paste the JSON and click button Beautify. For the secured API endpoints, you also need to provide authorization header. So you go to the headers tab right here, provide the key authorization and token value to perform a secure connection to your API. All right, that's it guys and see you in the next lesson.