How to Execute PUT Requests in Playwright

HOME

npm install playwright

import { defineConfig } from '@playwright/test';

export default defineConfig({
  reporter: 'html',
});

import { test, expect } from '@playwright/test';

 test('API Testing - PUT with Playwright', async ({ request }) => {

    // Perform a GET request
    const response = await request.get('https://jsonplaceholder.typicode.com/posts/1');
      
     // Check the response status code
     expect(response.status()).toBe(200);

     // Parse the response data
     const responseBody = await response.json();
     console.log(responseBody);
  
     // Perform a PUT request
     const putResponse = await request.put('https://jsonplaceholder.typicode.com/posts/1', {
       data: {
         title: 'Manager',
         body: 'Test',
         userId: 1
       }
     });

     // Check the response status code
     expect(putResponse.status()).toBe(200);

     // Parse the response data
     const putResponseBody = await putResponse.json();
     console.log(putResponseBody);

     // Validate the response
     expect(putResponseBody).toHaveProperty('title', 'Manager');
     expect(putResponseBody).toHaveProperty('body', 'Test');
     expect(putResponseBody).toHaveProperty('userId', 1);
   });
import { test, expect } from '@playwright/test';
test('API Testing - PUT with Playwright', async ({ request }) 
 // Perform a GET request
    const response = await request.get('https://jsonplaceholder.typicode.com/posts/1');
      
     // Check the response status code
     expect(response.status()).toBe(200);

     // Parse the response data
     const responseBody = await response.json();
     console.log(responseBody);

const putResponse = await request.put('https://jsonplaceholder.typicode.com/posts/1', {
       data: {
         title: 'Manager',
         body: 'Test',
         userId: 1
       }
 });
expect(putResponse.status()).toBe(200);
const putResponseBody = await putResponse.json();
console.log(putResponseBody);
expect(putResponseBody).toHaveProperty('title', 'Manager');
expect(putResponseBody).toHaveProperty('body', 'Test');
expect(putResponseBody).toHaveProperty('userId', 1);

npx playwright test api_put_tests.spec.ts

npx playwright show-report