Rayrun
← Back to Discord Forum

log API request to get details to be send in API request.

darshangdaveposted in #help-playwright
Open in Discord
darshangdave

Hi, is it possible to log the API request in playwright (console.log()) to see what exactly is being sent to the api endpoint? I am able to log the response (Object is APIResponse), but would need the request also to troubleshoot.

Thanks

This thread is trying to answer question "Is it possible to log the API request in Playwright to see what exactly is being sent to the api endpoint?"

4 replies

Not sure about console logs but if I'm not mistaken api requests are logged to traces.

darshangdave

Can anyone have idea on this? I am using the request context to call the API.

I am not sure if there is a way to have the request log to console, it would already be in the network traffic. But if you really require this, you could intercept the route, then console log the request after that fulfill the route. Have a look at Mocking requests and responses, you could use this in theory to intercept the request and pass it after logging it. But would be extra work, and you code may become hard to mainntain if not designed to handle this correctly. https://playwright.dev/docs/next/mock#modify-api-responses

This page suggests setting the DEBUG environment value:

DEBUG="pw:api"

Answer

Sure, you can log API requests in Playwright. It's a great way to see what's being sent to your API endpoint.

Intercepting Network Requests

Playwright lets you intercept and modify network traffic. You can use the page.route() method to handle network requests made by a page.

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

test('log API request', async ({ page }) => {
  await page.route('https://ray.run/api/endpoint', (route, request) => {
    console.log(request.url());
    route.continue();
  });
  await page.goto('https://ray.run');
});

Logging Request Details

Inside the callback function of page.route(), you can log the request details. For instance, you can print out the URL of each request with console.log(route.request().url()).

console.log(route.request().url());

Accessing More Request Details

If you need more details about the request, like its headers and payload data, you can use route.request().headers() and route.request().postData().

console.log(route.request().headers());
console.log(route.request().postData());

By logging these details, you'll see exactly what's being sent to your API endpoint. It's a handy tool for debugging or verifying your application is sending the correct data.

For more on API testing with Playwright, check out this blog post.

Related Discord Threads

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].