     [Answers](https://scrapfly.io/blog)   /  [playwright](https://scrapfly.io/blog/tag/playwright)   /  [How to capture background requests and responses in Playwright?](https://scrapfly.io/blog/answers/how-to-capture-xhr-requests-playwright)   # How to capture background requests and responses in Playwright?

 by [Bernardas Alisauskas](https://scrapfly.io/blog/author/bernardas) Apr 18, 2026 3 min read [\#playwright](https://scrapfly.io/blog/tag/playwright) [\#python](https://scrapfly.io/blog/tag/python) 

 [  ](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-capture-xhr-requests-playwright "Share on LinkedIn")    

 

 

When web scraping, it's often useful to monitor network requests. This enables retrieving crucial values found in response headers, body, or even cookies.

To better illustrate this, let's see what the request interception actually looks like using the below steps:

- Go to the login example on [web-scraping.dev/login](https://web-scraping.dev/login)
- Open the [devtools protocol](https://scrapfly.io/blog/answers/browser-developer-tools-in-web-scraping/) by pressing the `F12`
- Select the `Network` tab
- Fill in the login credentials and click login

After following the above steps, you will find each request event is captured, including its response details:



Above, we can observe the full details of the outgoing request. These details can be parsed to extract specific request-response values.



---

To allow Playwright get network requests and responses, we can use the `page.on()` method. This callback allows the headless browser to interept all network calls. Here's how to approach it using both Playwright's Python and NodeJS APIs:

Python

NodeJS

python```python
from playwright.sync_api import sync_playwright

def intercept_request(route, request):
    # We can update requests with custom headers
    if "login" in request.url:
        headers = request.headers.copy()
        headers["Cookie"] = "cookiesAccepted=true; auth=user123-secret-token;"
        route.continue_(headers=headers)
    # Or adjust sent data
    elif request.method == "POST":
        route.continue_(post_data="patched")
        print("patched POST request")
    else:
        route.continue_()

def intercept_response(response):
    # We can extract details from background requests
    if "login" in response.url:
        print(response.headers)
    return response

with sync_playwright() as pw:
    browser = pw.chromium.launch(headless=False)
    context = browser.new_context(viewport={"width": 1920, "height": 1080})
    page = context.new_page()

    # Intercept requesets and responses
    page.route("**/*", intercept_request)
    page.on("response", intercept_response)

    page.goto("https://web-scraping.dev/login")

    # Ensure login request header was successfully patched
    secret_message = page.inner_text("div#secret-message")
    print(f"The secret message is {secret_message}")
```





javascript```javascript
const { chromium } = require('playwright');

async function interceptRequest(route, request) {
    // We can update requests with custom headers
    if (request.url().includes("login")) {
        const headers = { ...request.headers(), "Cookie": "cookiesAccepted=true; auth=user123-secret-token;" };
        await route.continue({ headers });
    }
    // Or adjust sent data
    else if (request.method() === "POST") {
        await route.continue({ postData: "patched" });
        console.log("patched POST request");
    } else {
        await route.continue();
    }
}

async function interceptResponse(response) {
    // We can extract details from background requests
    if (response.url().includes("login")) {
        console.log(response.headers());
    }
    return response;
}

(async () => {
    const browser = await chromium.launch({ headless: false });
    const context = await browser.newContext({ viewport: { width: 1920, height: 1080 } });
    const page = await context.newPage();

    // Intercept requesets and responses
    await page.route("**/*", interceptRequest);
    page.on('response', interceptResponse);

    await page.goto("https://web-scraping.dev/login");

    // Ensure login request header was successfully patched
    const secretMessage = await page.innerText("div#secret-message");
    console.log(`The secret message is ${secretMessage}`);

    await browser.close();
})();
```







Here, we create two functions to capture background requests and responses. We override the cookie header for requests containing the `/login` endpoint. From the parsing results, we can see that the request was authorized, ensuring successful request modification.

Often, these background requests can contain important dynamic data. Blocking some requests can also reduce the bandwidth used while scraping, see our guide on [blocking resources in Playwright](https://scrapfly.io/blog/answers/how-to-block-resources-in-playwright) for more.

For further details on web scraping with Playwright, refer to our dedicated guide.

[Web Scraping with Playwright and PythonPlaywright is the new, big browser automation toolkit - can it be used for web scraping? In this introduction article, we'll take a look how can we use Playwright and Python to scrape dynamic websites.](https://scrapfly.io/blog/posts/web-scraping-with-playwright-and-python)



 

    



Scale Your Web Scraping

Anti-bot bypass, browser rendering, and rotating proxies, all in one API. Start with 1,000 free credits.

  No credit card required  1,000 free API credits  Anti-bot bypass included 

 [Start Free](https://scrapfly.io/register) [View Docs](https://scrapfly.io/docs/onboarding) 

 Not ready? Get our newsletter instead. 

 

## Explore this Article with AI

 [ ChatGPT ](https://chat.openai.com/?q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-capture-xhr-requests-playwright) [ Gemini ](https://www.google.com/search?udm=50&aep=11&q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-capture-xhr-requests-playwright) [ Grok ](https://x.com/i/grok?text=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-capture-xhr-requests-playwright) [ Perplexity ](https://www.perplexity.ai/search/new?q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-capture-xhr-requests-playwright) [ Claude ](https://claude.ai/new?q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-capture-xhr-requests-playwright) 



 ## Related Articles

 [  

 python headless-browser 

### Web Scraping Background Requests with Headless Browsers

In this tutorial we'll be taking a look at a rather new and popular web scraping technique - capturing background reques...

 

 ](https://scrapfly.io/blog/posts/web-scraping-background-requests-with-headless-browsers-and-python) [  

 curl 

### How to Use cURL GET Requests

Here's everything you need to know about cURL GET requests and some common pitfalls you should avoid.

 

 ](https://scrapfly.io/blog/posts/how-to-use-curl-get-requests) [     

 python screenshots 

### How to Track Web Page Changes with Automated Screenshots

There are many different ways to monitor web page changes and one of the most popular techniques is screenshot tracking....

 

 ](https://scrapfly.io/blog/posts/how-to-track-web-page-changes-using-automated-screenshots) 

  ## Related Questions

- [ Q How to capture background requests and responses in Puppeteer? ](https://scrapfly.io/blog/answers/how-to-capture-xhr-requests-puppeteer)
- [ Q How to block resources in Playwright and Python? ](https://scrapfly.io/blog/answers/how-to-block-resources-in-playwright)
- [ Q How to capture background requests and responses in Selenium? ](https://scrapfly.io/blog/answers/how-to-capture-xhr-requests-selenium)
- [ Q How to take a screenshot with Playwright? ](https://scrapfly.io/blog/answers/how-to-take-screenshot-with-playwright)
 
  



   



 Run headless browsers at scale, **1,000 free credits** [Start Free](https://scrapfly.io/register)