Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

90
Views
Node-fetch Promise response property prints as undefined

I know similar questions have been asked a bunch (see here and here), but I've tried the solutions mentioned there I'm trying to figure out why my node-fetch response isn't behaving as expected. Here's a simple version of the code:

 fetch(new URL("https://www.stackoverflow.com")).then(theResponse =>{
      console.log("This prints OK: "); 
      console.log(theResponse.headers);
      console.log("But this prints as undefined:");
      console.log(theResponse.headers["content-encoding"]);
      return theResponse;
})

The headers as printed are:

{
'accept-ranges': 'bytes',
'cache-control': 'private',
 connection: 'close',
'content-encoding': 'gzip',
 'content-security-policy': "upgrade-insecure-requests; frame-ancestors 'self' https://stackexchange.com",
etc.
}

Why is theResponse.headers["content-encoding"] returning as undefined?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You need to use the .get method of the Headers object:

console.log(theResponse.headers.get("Content-Encoding"));

Notice that unlike a Map or object property access, it works case-insensitively.

about 4 years ago · Juan Pablo Isaza Report

0

you can also use the method getAll of Headers object it return an array

console.log(theResponse.headers.getAll("Content-Encoding"));

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!