Skip to content
Discussion options

You must be logged in to vote

You can use the response event to access headers before the body is downloaded:

import got from 'got';

const request = got('https://example.com');

request.on('response', (response) => {
	console.log('Headers:', response.headers);
	console.log('Status:', response.statusCode);
	console.log('Content-Type:', response.headers['content-type']);
	
	// You can even abort the download if needed
	if (response.headers['content-length'] > 1000000) {
		request.destroy();
	}
});

const response = await request;

Alternatively, if you're using streams, the headers are available as soon as the response event fires:

const stream = got.stream('https://example.com');

stream.on('response', (response) => {
	c…

Replies: 1 comment

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
2 participants