0

I need to parse the cache-control header response to use it from caching the API response.

Sample response headers:

{"x-backside-transport":"OK OK","connection":"close","transfer-encoding":"chunked","access-control-allow-origin":"*","cache-control":"public, max-age=60","content-type":"application/json","date":"Thu, 05 Oct 2017 10:33:59 GMT","etag":"\"-161458570\"","x-powered-by":"Express","x-global-transaction-id":"429271565"}

I am using console.log("------ CACHE VALUE----"+JSON.stringify(response.headers.cache-control)); but this seems to fail everytime.

Giving me the error: ReferenceError: control is not defined

But when it try the same with

console.log("------ CACHE VALUE----"+JSON.stringify(response.headers.connection));

I am able to print out log value successfully. Am i missing something?

Rahul Kalidindi
  • 4,666
  • 14
  • 56
  • 92
  • Possible duplicate of [Unable to access JSON property with "-" dash](https://stackoverflow.com/questions/13869627/unable-to-access-json-property-with-dash) – Joe Oct 12 '17 at 12:06

1 Answers1

5

Dash - is not applicable in JSON. You may want to try parse like below

response.headers["cache-control"]
Hairus
  • 158
  • 1
  • 4