Rayrun
← Back to Discord Forum

Do a curl request with playwright

Hi, I have a test where I need the result from a curl request like: curl 'localhost/path/to/endpoint' -H 'Accept-Encoding: gzip, deflate, br' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Connection: keep-alive' -H 'DNT: 1' -H 'Origin: localhost' -H 'X-API-Key: 12345' --data-binary '{"datainfo"}' --compressed

Is this possible to do in playwright?

something like:

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

test.beforeEach(async ({ page }) => { await page.goto(HOMEPAGE_OPEN); });

test('test case description', async ({ page }) => { //Here the curl request action

...

// Here the interaction with the curl result }

This thread is trying to answer question "How to make a curl request with Playwright?"

9 replies

That's not playwright related but node related. You can do in Playwright pretty much anything that you can do in node. The answer is yes.

Something like this probably

const command = `your curl here`

  exec(command, (error, stdout, stderr) => {
    if (error) {
      console.error(`Error executing cURL command: ${error}`)
      return
    }

    if (stderr) {
      console.error(`cURL command returned an error: ${stderr}`)
      return
    }

    console.log(`cURL command executed successfully. Response: ${stdout}`)
  })

thanks for reply. i tried it out but it seems the code is not executed.

@passe_bln try this test('test case description', async ({ page }) => { const curlCommand = curl 'localhost/path/to/endpoint' -H 'Accept-Encoding: gzip, deflate, br' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Connection: keep-alive' -H 'DNT: 1' -H 'Origin: localhost' -H 'X-API-Key: 12345' --data-binary '{"datainfo"}' --compressed`;

const result = await page.evaluate(async (curlCommand) => { const response = await fetch(curlCommand, { compress: true }); const data = await response.json(); return data; }, curlCommand); expect(result).toEqual(/* expected value */); });`

@passe_bln: did you import exec?

did you import exec?

@skorp32: yep import is existing

yep import is existing

@yusufozt.: thanks for your reply. i get an error "the argument { compress: true } is not compatible to RequestInit"

thanks for your reply. i get an error "the argument { compress: true } is not compatible to RequestInit"

You should use the request fixture to make an http call

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].