DEV Community

Dhanush N
Dhanush N

Posted on

How do we access response headers in Javascript ?

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.

Oldest comments (5)

Collapse
 
ronnymedina profile image
Ronny Medina

Do you have an example of this?

Collapse
 
dhanushnehru profile image
Dhanush N • 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
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
 
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.