Navjot Singh
Navjot Singh

Reputation: 1

Accessing Request Headers on Chrome Console

I am trying to run a script to send a GET request and it requires some headers. I am testing the values on the Browser Console and verifying the values before running the script.

I am using this method to fetch the values.

localStorage.getItem('accessToken');

Console:

Console Snapshot

Headers:

Headers Snapshot

What am I doing wrong? How can I access the request headers values

I also checked in the Application/localStorage folder on Chrome, and I don't see these headers. Where else are they stored and how can I fetch them?

I have tried using both localStorage and sessionStorage.

Upvotes: 0

Views: 73

Answers (1)

Marvin
Marvin

Reputation: 953

You are conflating localStorage, which is data saved across browser sessions, sessionStorage which is data stored in a single browser session, and browser headers. Your data is stored in the headers, but you are trying to retrieve them from completely separate storage systems.

To get the headers in JavaScript, use the Headers object.

const myHeaders = new Headers(); // Currently empty
const accessToken = myHeaders.get("accessToken");
const authorizationToken = myHeaders.get("authorizationToken");
const correlationId = ...
// ...

One additional note: you use var which is old JavaScript syntax. Since ES6, released in 2015, variables should use const for those that don't change and let for those that do change.

Upvotes: 0

Related Questions