DEV Community

loading...

How do we access response headers in Javascript ?

dhanushxeno profile image DHANUSH N ・1 min read

I am setting a correlation Id in node js application & I need to access it in the frontend to pass in the request again. Not sure how to access.

Discussion (5)

pic
Editor guide
Collapse
jordanfinners profile image
Jordan Finneran • Edited

It would depend what you are using on the frontend to make the request.
If you are using the fetch API (native in all browsers except IE) you can access the response headers
developer.mozilla.org/en-US/docs/W...

I would probably look for a monitoring tool to do this for you though, like datadog or AWS Cloudwatch or Azure Applications Insights.

Collapse
valeriavg profile image
Valeria

What are you using in nodejs?
Built-in http server has res.setHeader, while express has res.header method for that.

Collapse
ronnymedina profile image
Ronny Medina

Do you have an example of this?

Collapse
dhanushxeno profile image
DHANUSH N Author • Edited

In my node server
I have did like res.set( correlationid, 12345)
I need to access the value from frontend react, response.headers in frontend not able to get the correlationid

Collapse
ronnymedina profile image
Ronny Medina

Do you want to set headers on the node?, maybe this example could be used
stackoverflow.com/questions/767626...

or

// https://www.geeksforgeeks.org/express-js-res-set-function/
res.set({ 'correlationid': '12345'}); 
Enter fullscreen mode Exit fullscreen mode