Node-fetch: "TypeError: Cannot read property 'toLowerCase' of undefined" when calling headers.getAll()

Created on 25 Aug 2016  路  6Comments  路  Source: node-fetch/node-fetch

Possibly I'm using the API very wrong, possibly needs another null/string check before calling .toLowerCase()...

Usage:

const fetch = require('node-fetch');

function getMeta(url) {
  return fetch(url, {method: 'HEAD'})
    .then((res) => {
      console.log(res.headers.getAll());
      return res.headers;
    });
}

getMeta('https://cdn.meme.am/instances/58480692.jpg')
  .then((res) => console.log(JSON.stringify(res, null, 2)))
  .catch(console.error);

Output:

TypeError: Cannot read property 'toLowerCase' of undefined
    at Headers.has (/Users/pdehaan/dev/tmp/del/node_modules/node-fetch/lib/headers.js:121:42)
    at Headers.getAll (/Users/pdehaan/dev/tmp/del/node_modules/node-fetch/lib/headers.js:65:12)
    at fetch.then (/Users/pdehaan/dev/tmp/del/fetch-head.js:6:31)
    at process._tickCallback (internal/process/next_tick.js:103:7)

Source:

/**
 * Return all header values given name
 *
 * @param   String  name  Header name
 * @return  Array
 */
Headers.prototype.getAll = function(name) {
    if (!this.has(name)) {
        return [];
    }

    return this._headers[name.toLowerCase()];
};

— via ./lib/headers.js:64-70

I think I was incorrectly assuming that getAll() would return me a simple object containing all the name/value pairs for the headers (sort of like raw(), but without the extra array nesting).

All 6 comments

getAll is meant to return an array with the values of a given header key
Set-Cookie is one very common header that can appear more then once for example (http://stackoverflow.com/a/6375214/1008999)
Calling .getAll('Set-Cookie') should return an array of all those values


Looking at what you get from requesting https://github.com/bitinn/node-fetch/issues/ show you that you got two Vary headers for example
skarmavbild 2016-08-25 kl 22 06 17

so the result from calling .getAll('Vary') should yield:

["Accept-Encoding", "X-PJAX"]

It's a good idea to tryout the browsers fetch version first before you come to any conclusion that node-fetch is doing something wrong

If you would like to get all headers & keys you would have to something like

for(let [key, value] of res.headers.entries()) {
  console.log(key, value)
}

// could also do:
console.log( Array.from( res.headers.entries() ) )

Sorry, I was trying to figure out browser fetch() but was getting a bunch of CORS errors and unexpected results.

I tried using entries(), but it's giving me an error:

const fetch = require('node-fetch');

function getMeta(url) {
  return fetch(url, {method: 'HEAD'})
    .then((res) => res.headers.entries());
}

getMeta('https://github.com/bitinn/node-fetch/issues/')
  .then((headers) => console.log(Array.from(headers)))
  .catch(console.error);
TypeError: res.headers.entries is not a function
    at fetch.then (/Users/pdehaan/dev/tmp/del/generator-ra/fetch-head.js:5:32)
    at process._tickCallback (internal/process/next_tick.js:103:7)

Possibly related to #127

hmm, yea wasn't aware of the missing #127 header methods...

node-fetch doesn't have ES6 related feature yet, but we do have res.headers.raw() as a workaround. It gives you the internal header object (it was added before entries() was an official API)

For lack of ES6 features we will follow up the issue in #127 (also added to KNOWN LIMIT document before we can support them).

Was this page helpful?
0 / 5 - 0 ratings