question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Missing header in page.on("response")

See original GitHub issue

Steps to reproduce

Tell us about your environment:

  • Puppeteer version: 1.14
  • Platform / OS version: Linux Ubuntu
  • URLs (if applicable):
  • Node.js version: 10

What steps will reproduce the problem?

Please include code that reproduces the issue.

  1. Launch server.js
const http = require("http");

const hostname = "127.0.0.1";
const port = 5000;

const server = http.createServer((req, res) => {
  if (req.url === "/one") {
    res.statusCode = 200;
    res.setHeader("Content-Type", "text/html");
    res.setHeader("set-cookie", "_one=1");
    res.end('<html><script src="http://127.0.0.1:5000/two"></script></html>');
  } else if (req.url === "/two") {
    res.statusCode = 200;
    res.setHeader("Content-Type", "application/javascript");
    res.setHeader("set-cookie", "_two=2");
    res.end(`console.log('Hello two');`);
  }
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});
  1. Launch this puppeteer script
const puppeteer = require("puppeteer");

(async () => {
  const browser = await puppeteer.launch({
    headless: true
  });
  const page = await browser.newPage();

  page.on("response", response => {
    console.log("New response:");
    console.log(response.url());
    console.log(response.headers());
  });

  await page.goto("http://127.0.0.1:5000/one", {
    waitUntil: "load"
  });

  await browser.close();
})();

What is the expected result? We should get the set-cookie headers for both requests.

What happens instead?

New response:
http://127.0.0.1:5000/one
{ 'content-type': 'text/html',
  'set-cookie': '_one=1',
  date: 'Mon, 09 Sep 2019 09:59:48 GMT',
  connection: 'keep-alive',
  'content-length': '62' }
New response:
http://127.0.0.1:5000/two
{ date: 'Mon, 09 Sep 2019 09:59:48 GMT',
  connection: 'keep-alive',
  'content-length': '25',
  'content-type': 'application/javascript' }

The second set-cookie header is missing.

We can add this lines to the end of the script to confirm the second set-cookie header was successfully interpreted by the browser (but no visible in the response event).

  const client = await page.target().createCDPSession();
  console.log(await client.send("Network.getAllCookies"));

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:8

github_iconTop GitHub Comments

10reactions
robertm-97commented, Aug 3, 2020

I had this same issue but I managed to find a workaround. The solution is to listen on the Network.responseReceivedExtraInfo event suggested by https://github.com/puppeteer/puppeteer/issues/4918#issuecomment-539454771 and extract the cookies from there. I’ve created a class that collects the cookies set in request and looks something like this

const setCookie = require('set-cookie-parser');

class HttpCookieInterceptor {
    foundCookies = [];

    httpSetCookiesKey = 'set-cookie';

    constructor(client) {
        // Setup listener for handling finished request
        client.on('Network.responseReceivedExtraInfo', (response) => this.captureHttpCookies(response));
    }

    static async create(page) {
        // open sessions to DevTools
        const client = await page.target().createCDPSession();
        await client.send('Network.enable');
        // Setup request interceptor rules, in this case we will intercept all request
        await client.send('Network.setRequestInterception', {
            patterns: [{
                urlPattern: '*',
            }],
        });
        await client.on('Network.requestIntercepted', async (e) => {
            // Let the request continue, we don't need anything from here
            await client.send('Network.continueInterceptedRequest', {
                interceptionId: e.interceptionId,
            });
        });
        return new HttpCookieInterceptor(client);
    }

    captureHttpCookies(request) {
        if (request && request.headers && request.headers[this.httpSetCookiesKey]) {
            const cookieString = request.headers[this.httpSetCookiesKey];
            const httpCookies = setCookie.parse(cookieString);
            this.foundCookies = [...this.foundCookies, ...httpCookies];
        }
    }

    getCookies() {
        return this.foundCookies;
    }
}

It can be used like this:

const browser = puppeteer.launch({
        headless: true,
        devtools: true,
});
const page = await browser.newPage();
const httpCookieCollector = await HttpCookieInterceptor.create(page);
// do some navigation here
const httpCookies = httpCookieCollector.getCookies();

NOTE: This collects all cookies, you will need to filter duplicates after Hope this helps

4reactions
kdzwinelcommented, Oct 8, 2019

I’ve also run into this and can reproduce it in v1.20. It looks like puppeteer is not listening to the Network.responseReceivedExtraInfo event that contains the raw headers (you can do that yourself to work around the issue). DevTools are doing just that:

set-cookie-extra

It also might be a Chromium bug since this cookie is not really blocked, so IMO it should appear in the Network.responseReceived headers.

Read more comments on GitHub >

github_iconTop Results From Across the Web

`xhr.getAllHeaders()` is missing some headers in a browser ...
In a script running on the popup page, I am making an ajax request. On the first line of my handler for the...
Read more >
HTTP response status codes - MDN Web Docs - Mozilla
HTTP response status codes indicate whether a specific HTTP request has been successfully completed. Responses are grouped in five classes:
Read more >
Mitigating framesniffing with the X-Frame-Options header
Double-click the HTTP Response Headers icon in the feature list in the middle. In the Actions pane on the right side, click Add....
Read more >
HTTP/1.1: Status Code Definitions
There are no required headers for this class of status code. Since HTTP/1.0 did not define any 1xx status codes, servers MUST NOT...
Read more >
URL Inspection Tool - Search Console Help - Google Support
Troubleshoot a missing page: There can be many reasons why a page hasn't been ... including a screenshot and HTTP response headers, click...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found