Post API Request using Playwright

Profile picture for user arilio666

In our previous article, we discussed how to Get API Requests using Playwright. Now, Let's create a POST request using Playwright.

Table of Contents

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

Demo API

For the demo, let's use Create user POST API of the reqres website.

Post API Request using Playwright

How to Create Post Request

Step 1: Use Request Instance as Parameter

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

Step 2: Create HTTP Post Request and store response in a variable

 const response = await request.post("https://reqres.in/api/users")

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

Step 3: Add Request Body to Post Method

const response = await request.post("https://reqres.in/api/users",{
    data: {
        "name": "morpheus",
        "job": "leader"
    }
})

Step 4: Verify Response code is 201

expect(response.status()).toBe(201)

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

console.log(await response.json())

Code:

test.only('Post Request', async({request}) =>{
    const response = await request.post("https://reqres.in/api/users",{
        data: {
            "name": "morpheus",
            "job": "leader"
        }
    })
    expect(response.status()).toBe(201)
    console.log(await response.json())
})

Video Tutorial: Playwright Post API Request