How to set the host header in playwright

Viewed 1386

I have a need to be able to change the host header for a url. I've tried setting extra headers but I keep getting an invalid argument error.

from playwright.sync_api import sync_playwright    

def get_title():
    with sync_playwright() as playwright:

        browser = playwright.chromium.launch(headless=True)
        context = browser.new_context(
            user_agent="testing",
        )
        context.set_extra_http_headers({"host": "example.com"})
        page = context.new_page()
        page.goto(url="http://111.222.333.444", wait_until="domcontentloaded")

        title = page.inner_html(selector="title")

        context.close()
        browser.close()

        return title

Setting the header to the page or the context give the same error. Is it possible to set the host header with playwright?

1 Answers

Chromium will not allow you to override the Host header. I presume this is because Host is something the browser manages internally. Source.

Why do you want to modify this header?

Related