Put API Request using Playwright

Profile picture for user arilio666

This article will show how we can PUT request using playwright.

  • A PUT request is an HTTP request method to update or replace an existing resource on a web server. 
  • The PUT request requires the client to send the updated resource representation to the server. 
  • This means that the server will completely replace the existing resource with the new one sent by the client.

Demo API

Let's use the PUT API of the reqres website for the demo.

How to Create PUT Request

Step 1: Use the request instance as a parameter.

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

Step 2: Send HTTP PUT Request along with the data to be updated and get the response body.

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

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

     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.put('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()

           });
   });