puppeteer-sharp icon indicating copy to clipboard operation
puppeteer-sharp copied to clipboard

Impossible to add a header to a Websocket request

Open tarockx opened this issue 2 years ago • 1 comments

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.

tarockx avatar Aug 31 '21 16:08 tarockx

It seems it's not supported upstream https://github.com/puppeteer/puppeteer/issues/2974

kblok avatar Dec 17 '21 19:12 kblok