Event before body is downloaded, but after headers are fetched #2281
-
|
Hi there, i'm looking for a way to inspect response headers before the response body is downloaded. Was looking around documentation, examples and the code, bu was unable to find anything. any help highly appreciated |
Beta Was this translation helpful? Give feedback.
Answered by
sindresorhus
Oct 11, 2025
Replies: 1 comment
-
|
You can use the 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 const stream = got.stream('https://example.com');
stream.on('response', (response) => {
console.log('Headers:', response.headers);
// Body hasn't been downloaded yet
});
stream.pipe(someWriteStream);The |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
sindresorhus
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can use the
responseevent to access headers before the body is downloaded:Alternatively, if you're using streams, the headers are available as soon as the
responseevent fires: