Sunday 9 July 2023

Playwright: How to add custom headers?

Using 'page.setExtraHTTPHeaders()' method, we can set the extra HTTP headers, these will be sent with every request the page initiates.

 

Example

Map<String, String> headers = new HashMap<>();
headers.put("header1", "value1");
headers.put("header2", "value2");
headers.put("header3", "value3");

page.setExtraHTTPHeaders(headers);

Find the below working application.

 

CustomHeaders.java

package com.sample.app.miscellaneous;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

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

public class CustomHeaders {

	public static void main(String[] args) throws IOException {
		try (Playwright playwright = Playwright.create()) {
			Browser browser = playwright.chromium()
					.launch(new BrowserType.LaunchOptions().setHeadless(false).setSlowMo(100));
			Page page = browser.newPage();

			Map<String, String> headers = new HashMap<>();
			headers.put("header1", "value1");
			headers.put("header2", "value2");
			headers.put("header3", "value3");

			page.setExtraHTTPHeaders(headers);
			page.navigate("https://self-learning-java-tutorial.blogspot.com/");

		}
	}

}





 

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment