Skip to content
Discussion options

You must be logged in to vote

You can access cookies through the set-cookie header in the response:

const response = await got('https://example.com');
const cookies = response.headers['set-cookie'];

console.log(cookies); // Array of cookie strings

If you need to parse them into a more useable format, you can do something like:

const {headers} = await got('https://example.com');
const cookieStrings = headers['set-cookie'] || [];

const cookies = cookieStrings.reduce((acc, cookie) => {
	const [keyValue] = cookie.split(';');
	const [key, value] = keyValue.split('=');
	acc[key] = value;
	return acc;
}, {});

console.log(cookies); // {cookieName: 'cookieValue', ...}

For more advanced cookie handling (like preserving cooki…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@LeonAlvarez
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by sindresorhus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants
Converted from issue

This discussion was converted from issue #1702 on April 28, 2021 07:29.