Patch API Request using Playwright

Profile picture for user arilio666

In this article, we will see how to use patch requests using Playwright.

  • A PATCH request is an HTTP method used to update an existing resource on a web server partially. 
  • APIs can use PATCH requests to modify or update existing resources without replacing the entire resource.

Demo API

For the demo, let's use PATCH API of the reqres website.

Step 1: Use the request instance as a parameter.

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

Step 2: Send HTTP PATCH Request along with the data to update.

 const response = await request.patch('https://reqres.in/api/users/2',{
       data: {
           name: "John Wick",
           job: "Assasin"
       },
       })

Step 3: Verify the status code of the response is 200 and also verify the response body.

 const responseBody =  JSON.parse(await response.text())
     console.log(responseBody)
     expect(response.status()).toBe(200)
expect(responseBody.name).toBe('John Wick')
expect(responseBody.job).toBe('Assasin')
expect(responseBody.updatedAt).toBeTruthy()

Code:

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

       
       const response = await request.patch('https://reqres.in/api/users/2',{
       data: {
           name: "John Wick",
           job: "Assasin"
       },
       })
       
     const responseBody =  JSON.parse(await response.text())
     console.log(responseBody)
     expect(response.status()).toBe(200)
expect(responseBody.name).toBe('John Wick')
expect(responseBody.job).toBe('Assasin')
expect(responseBody.updatedAt).toBeTruthy()

           });
   });
    
  • We can see that the request has been updated successfully.