Skip to content

Impossible to add a header to a Websocket request #1795

@tarockx

Description

@tarockx

Description

I need to add a custom HTTP header to a WebSocket request. However, it looks like puppeteer-sharp is unable to do so.
In fact, from the test I've done, it seems there is no way to interact with WebSocket requests at all... am I wrong?

At first, I tried just doing this:

await page.SetExtraHttpHeadersAsync(new Dictionary<string, string>() {
    { "Authorization", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" }
});

But from the page response I could tell the header was only being set on normal http/https requests, but not on websocket (ws:// and wss://) requests.

So I tried to hook into the Request event:

page.Request += (s, e) => {
    if (e.Request.Url.StartsWith("ws"))
    {
        e.Request.Headers.Add("Authorization", "xxxxxxxxxxxxxxxxxxxxxxx");
    }
};

however, the if statement is never entered, suggesting that ws:// and wss:// requests are actually not captured by the event.

Am I missing something, or is any manipulation of websocket requests just not supported? Is there a workaround for this?

Complete minimal example reproducing the issue

This minimal code example will show how WebSocket requests are not captured. You will notice that, even though there is a wss:// connection on the page, it is not logged to the console.

private static async Task TestChromeAsync()
{
    var browserFetcher = new BrowserFetcher();
    await browserFetcher.DownloadAsync();
    var browser = await Puppeteer.LaunchAsync(
        new LaunchOptions { 
            Headless = false,
            Devtools = true
        }
        );
    var page = await browser.NewPageAsync();
    page.Request += (s, e) => {
        Console.WriteLine(e.Request.Url);
    };

    await page.GoToAsync("https://websocketstest.com/");
}

Expected behavior:

  • We should be able to intercept ws:// and wss:// requests with the Page.Request event
  • Setting extra headers via Page.SetExtraHttpHeadersAsync() should also affect websockets

Actual behavior:

WebSocket requests are ignored

Versions

Tested with both latest version (v5.0.0.0) and older 4.xx versions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Upstream Change neededWe need to wait for changes in Chromium or Puppeteer (Node.JS)

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions