Advertisement
RahulDevGarg

POST using Playwright

Oct 30th, 2023
841
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const { chromium } = require('playwright');
  2.  
  3. (async () => {
  4.   // Launch a browser and create a page
  5.   const browser = await chromium.launch();
  6.   const page = await browser.newPage();
  7.  
  8.   // Define the API endpoint and request data
  9.   const apiEndpoint = 'https://example.com/api/endpoint'; // Replace with your API URL
  10.   const body = {
  11.     key1: 'value1',
  12.     key2: 'value2',
  13.   };
  14.  
  15.   // Configure the POST request
  16.   const headers = {
  17.     'Content-Type': 'application/json',
  18.   };
  19.   const requestOptions = {
  20.     method: 'POST',
  21.     headers,
  22.     body: JSON.stringify(body),
  23.   };
  24.  
  25.   // Make the POST request
  26.   const response = await page.goto(apiEndpoint, requestOptions);
  27.  
  28.   // Check the response status
  29.   if (response.status() >= 200 && response.status()<400) {
  30.     const responseBody = await response.json();
  31.     console.log('Response:', responseBody);
  32.   } else {
  33.     console.error('Failed to make the API request');
  34.   }
  35.  
  36.   // Close the browser
  37.   await browser.close();
  38. })().catch((error) => console.error(error));
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement