Get API Request using Playwright

Profile picture for user arilio666

We can also use Playwright to access our application's REST API. In this article, let's start with the Get request.

Table of Contents

  1. Demo API
  2. How to Create Get Request
  3. Video Tutorial

Demo API

For the demo, let's use single user GET API of the reqres website.

Get API Request using Playwright

How to Create Get Request

Step 1: Use the request instance as a parameter.

test.only('Get API Request', async({request}) =>{
}

Step 2: Send HTTP Get Request and store response in a variable.

const response = await request.get('https://reqres.in/api/users/2')

Add request value highlighted in screenshort to URL of the website.

Step 3: Verify the status code of the response is 200.

expect(response.status()).toBe(200);

If you want to print the response in the console, use the code below.

console.log(await response.json());

Code

test.only('Get API Request', async({request}) =>{
    const response = await request.get('https://reqres.in/api/users/2')
    expect(response.status()).toBe(200);
    console.log(await response.json());
}) 

If you compare the code with your other tests, you will find that code is identical. We have replaced the page with request and goto with the get method.

Video Tutorial: Playwright Get Request