Post Request | Bondar Academy
Course: Playwright API Testing with TypeScript
Module: API Testing Basics
Instructor: Artem Bondar
Lesson Summary
In this lesson, we learned how to make a POST request using Playwright . Here are the key points covered: Overview of POST Request Previously, we used Postman to create an article by providing: The API URL The request body for the article An authorization header obtained from the browser's network tab Obtaining the Authorization Token To automate API calls without a browser, we need to retrieve the authorization token differently: Inspect user details and log out. Sign in again to capture the login request in the network tab. Extract the token from the response payload. The token is saved in local storage as a JWT token and is used for all subsequent API requests. Simulating the Request in Postman We created a new request in Postman to obtain the token: POST {API_URL} Body: {request_body} Upon sending the request, we received a unique token each time. Implementing in Playwright To recreate this in Playwright: Create a new test for creating an article. Use the request fixture to make a POST request. Pass the API URL and the request body as an object. Extract the token from the response JSON. Summary To make a POST request in Playwright: Call the POST method with the API URL and an object containing the request body. Extract the JSON response and retrieve the auth token . In the next lesson, we will use this token to create an article with another POST request.
Video Transcript
Hello, guys. We're moving on. In this lesson, I will show you how to make a POST request using Playwright. So let's get into it. So in the previous lesson, we were making a POST request to create the article using a Postman right here. So we provided the API URL, the request body of the article that we want to create, and also since it was authorized request, we had to provide authorization header right here. We got this header from the browser, just inspecting the browser networking tab and found this header over there. But question, where will we get this header from if we will not use browser? Because in our API automation, we do not use browser, we just make API calls. So we need to get this value somehow differently. So let's go to test application and explore how we can obtain the value of authorization token. So I inspect the user details, edit profile, and just click on Logout right here. Then going to inspect networking tab, and let me sign in one more time with my test user, pwapiuser at test.com, and the password is welcome, and click Sign In. We were signed in and look what happened in the networking tab. So we had a login request right here to this API URL. This was a post request, the response status was 200, and the payload was our authorization credentials. This is a request body, and in the response, we have this response object with user details, email, username, and here we go. We have a property token with a token value. So this is where browser get this value from, and then it save this value. Let me show you here in application in the local storage. Here it's saved as JWT token, and then browser use this value adding it to a header for all API requests that browser is doing to our API. This is something what we need to simulate as well. What we need to do is to make a post request to this API endpoint with this payload to get the response of our token. So let's simulate this in the Postman first, and then transfer it to the Playwright. So I create a new request, getting this URL, going back to the Postman, pasting the URL, then we need body, which is a raw. I'm copying a payload from here, view source, let me do it like this, pasting it right here, and this is a post request, changing to the post request. Click Send, and here we go, it's working successfully. You can see we get a response and with a token. Every time I send this request, every time our API generates a unique value of the token. This is how it's supposed to be. Then we use this token to interact with our API as authorized user. So let's recreate this scenario in the Playwright. So going back to our project and let's start a new test, which I call test create article, because later we will use this to create the article. Async, then we need a request fixture and our test body. Now, as usual, we start with constant creation. So let's create a constant token response equals to await request dot, and then we're looking for post requests right here. The first argument should be as usual, the API URL that we want to request. So going back to Postman, copying right here, and pasting it right here. So far, it's very similar to get request like we did before. Now, how to add body to this request? Inside of the post method right here, you put the comma at the end and add the object. So inside of this object, you will put all different parameters for your request. To more conveniently work with this code, I put it on the next line. Then the property inside of this object where we need to put our request body called data, data column. Then here, you put the request object from the Postman. Going back to Postman, this is our request object, Control C and then paste it, Control V. Yeah, that's pretty much it. Let's try if it is working. Yeah, it seems it is working. Now, let's extract the response object from here. So const token response JSON equals awaits token response.json. Let's print this out to the console. Console.log token response JSON. Run the test and we have printed the response successfully. Look at this. So this is exactly as we saw in the response in the Postman. So now what is left is just to read the value from this object hidden in the property token under the user object. So let's do this. Creating new constant, const auth token, let me call it like that, equals to token response JSON dot, it is a user property dot user dot, and then it is a token property dot token. That's it. Then auth token printed out to the console. Let's see if it is working. Running the test and here we go. It's working successfully. We got the value of the token. So that's pretty much it. So this is how you do a POST request in Playwright. Let's do a quick summary what we did in this lesson. So to make a POST request, you call a POST method inside of the request fixture. First argument, you provide the API URL, and for the second argument, you put the object that can have different properties as a part of your request. The property with the name data is where you put your request body. So we put the request body over here, made the request, then the standard operation of extracting the JSON object from the response, and then we are reading the token property inside of this JSON response to extract the auth token, and that's it. So in the next lesson, we will continue working with the script, creating the article, and we will pass the value of authorization token into the header creating the article with another POST request. All right, so see you in the next lesson.