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.

Handle cookies in request interceptor when cors request was happened

See original GitHub issue

Steps to reproduce

Tell us about your environment:

  • Puppeteer version: 1.15.0
  • Platform / OS version: OSX 10.14.4
  • Node.js version: 10.4.1

What steps will reproduce the problem? Try run next code:

        await page.goto('http://localhost:3000');
        await page.setRequestInterception(true);

        await page.setCookie({name: 'userId', value: 'my-user-id'});

        page.on('request', interceptedRequest => {
            if (interceptedRequest.url().endsWith('/some-api')) { //send cors request to localhost:8080/some-api
                const cookie = interceptedRequest.headers()['Cookie']; //undefined
                expect(cookie).toContain('userId'); //wrong, because cookie header is undefined
                interceptedRequest.continue();
            }
        });

With next puppeteer configuration:

    launch: {
        dumpio: process.env.HEADLESS === 'false',
        headless: process.env.HEADLESS !== 'false',
        slowMo: 20,
        args: [
            '--disable-web-security',
            '--disable-features=IsolateOrigins,site-per-process',
            '--disable-site-isolation-trials'
        ]
    }
  1. Run code above
  2. Cookie is always empty

What is the expected result? Expect that cookie should contain in request headers

What happens instead? Cookie header is undefined

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:7

github_iconTop GitHub Comments

8reactions
evanrolfecommented, Jul 18, 2019

@lavcraft I’m currently using a work-around for this bug by storing the cookies from the page in a variable like this:

const cookies = page.cookies().map((cookie) => { return `${cookie.name}=${cookie.value}`; }).join('; ');

You can then use them in with the intercepted request i.e.

page.on('request', interceptedRequest => {
  const headers = interceptedRequest.headers();
  headers.cookie = cookies;
  
  console.log(headers);

  interceptedRequest.continue();
});
1reaction
linthertosscommented, May 27, 2021

Can’t seem to get cookies from interceptedRequest.headers(); But if you want to get all cookies from page, So you can:

page._client.send('Network.getAllCookies')

It return all cookies from your page

Read more comments on GitHub >

github_iconTop Results From Across the Web

angular - How to send "Cookie" in request header for all the ...
Following this (and if the authentication is successful), the server will return a cookie in the response. The browser will store this cookie...
Read more >
Handling Cookies with Spring Boot and the Servlet API
This article demonstrates how to handle cookies in a Spring Boot or Servlet-based application.
Read more >
Top 10 ways to use Interceptors in Angular - Medium
My ten favorite ways to use the interceptor in Angular. With examples of error handling, profiling, caching and more this list will inspire...
Read more >
What is CORS (cross-origin resource sharing)? - PortSwigger
These headers state that access is allowed from the requesting domain ( malicious-website.com ) and that the cross-origin requests can include cookies ......
Read more >
Angular Interceptors to Manage HTTP Requests
A similar process happens when the server replies. We now have the response being intercepted by the HTTP interceptor, allowing us to perform...
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