How to get network call requests in Playwright?

When a user uses a web application, the browser and server exchange HTTP requests and responses. These network calls are important for fetching data, loading resources, and executing application logic. It is necessary to manage and understand these network calls for several reasons, like debugging and optimizing performance. In this article, we will discuss how to use Java to get network call requests in Playwright.

We can easily capture network requests or logs using the browser developer tools. Most modern web browsers have built-in developer tools that allow us to view and capture network requests.

We can access the developer tools by right-clicking on the webpage and selecting the “inspect” option.

"Inspect" option

Selecting the “Inspect” option will open up the browser developer tools, where we can see a bunch of tabs, including the Network tab. We can click on the “Network” tab to see and capture the network calls.

Network tab in developer tools

Next, let’s explore how we can capture network call requests using Playwright.

Capture network call requests using Playwright

Playwright’s Page interface provides an onRequest() method, which is fired every time a page issues a new request. So, we can use this method to capture the network request calls.

		Playwright playwright = Playwright.create();

		// Launching the browser
		Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));

		// creating a BrowserContext
		BrowserContext browserContext = browser.newContext();

		// creating the page
		Page page = browserContext.newPage();

		// capturing the network call requests
		page.onRequest(request -> { /* write code here */ });
Get the request method of a network request

To obtain the method of a request, we can utilize the method() function provided by the Request interface.

page.onRequest(request -> { System.out.println("Request Method:  " + request.method()); });
Get the request URL

The request URL of a network call can be fetched using the url() method.

		page.onRequest(request -> { System.out.println("Request URL: " + request.url()); });
Get the request payload

We can payload ( or, we can say, request body ) of a network call request using the postData() method.

page.onRequest(request -> { System.out.println("Request Payload: " + request.postData()); });

If there is no request body, the function postData() will return null.

Get the request headers
  • “headers()” method provides the headers for a network call request. It returns a Map<String, String> with all header names in lowercase.
  • The map’s key corresponds to the header name, while the value represents the header’s value.
  • This method doesn’t return the security-related headers, including cookie-related ones. We can use the “allHeaders()” method for a complete list of headers, including cookie information.
		// excluding security-related headers
		page.onRequest(request -> { System.out.println("Request Headers: " + request.headers()); });
		
		// getting a complete list of headers
		page.onRequest(request -> { System.out.println("Request Headers: " + request.allHeaders()); });

Here is the whole code

import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;

public class CodekruTest {

	public static void main(String[] args) {

		Playwright playwright = Playwright.create();

		// Launching the browser
		Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));

		// creating a BrowserContext
		BrowserContext browserContext = browser.newContext();

		// creating the page
		Page page = browserContext.newPage();

		// capturing the network call requests
		page.onRequest(request -> {
			System.out.println("Method: " + request.method() + " , Url: " + request.url() + " , Payload: "
					+ request.postData() + ", Headers: " + request.headers());
		});

		page.navigate("https://www.makemytrip.com");
		browser.close();
		playwright.close();

	}

}

We hope that you have liked the article. If you have any doubts or concerns, please write to us in the comments or mail us at admin@codekru.com.

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *