• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

315
Views
How to retrieve an http request headers item from a symbol map?

I am trying to retrieve a particular request header from an apolloserver request (i.e. the request object from a GraphQLRequestContext), inside a a plugin.

logging the headers object logs out as:

Headers {
  [Symbol(map)]: [Object: null prototype] {
    'content-type': [ 'application/json' ],
    user: [
      '{...(redacted)...}'
    ],
    'x-header-foo': [ '234' ],
    accept: [ '*/*' ],
    'content-length': [ '256' ],
    'user-agent': [ 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)' ],
    'accept-encoding': [ 'gzip,deflate' ],
    connection: [ 'close' ],
    host: [ 'localhost:3000' ]
  }
}

but headers['x-header-foo'] yields undefined. How do I get the header value? I have not used symbols much so far.

almost 3 years ago · Juan Pablo Isaza
1 answers
Answer question

0

As you said rightly, Headers are Symbols and not Objects, so to get value, you use the get method

Read more about Symbols here Symbol is a built-in object whose constructor returns a symbol primitive — also called a Symbol value or just a Symbol — that's guaranteed to be unique.

Headers are Symbols and not Objects, so to get value, you use the get method

const host = req.headers.get("host"); // stackoverflow.com

If you wish to destructure using Symbols:

let symbol = Symbol()
let obj = { [symbol] : 'value'}
let { [symbol]: alias } = obj

console.log(alias)
almost 3 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 Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error