Apify Discord Mirror

Updated last year

Set cookies with cheerio

At a glance

The community member switched from Playwright to Cheerio due to familiarity, but is having trouble setting cookies (specifically for authentication) in Cheerio. In Playwright, they used context.page.context().addCookies, but they are unsure how to achieve the same in Cheerio.

Another community member provided two options: 1) adding the cookies to the request headers as a cookie string, either during enqueueing or preNavigationHooks, and 2) setting the cookies through the session using session.setCookies, which needs to be done in preNavigationHooks because the session is not available during enqueueing. The second community member indicated that this was the solution they were looking for.

Useful resources
I switched from playwright to cheerio because of familiarity and cannot figure out how to set cookies (specifically for auth).

In playwright I did ontext.page.context().addCookies. How do I do this in cheerio?
L
J
2 comments
One option is to simply add them to request.headers as cookie string either during enqueueing or preNavigationHooks.

Second option is to do it through session as session.setCookies https://crawlee.dev/api/core/class/Session#setCookies. This needs to be done in preNavigationHooks because you don't have the attached session in enqueueing yet
That's what I was looking for, thanks!
Add a reply
Sign up and join the conversation on Discord