How do I get http headers in React.js How do I get http headers in React.js reactjs reactjs

How do I get http headers in React.js


It's not possible to access page headers via client JavaScript. You can get these request headers on your server side and then pass them into index.html of your React app. For example:

//in index.html<head>...  <script>    window.__INITIAL_HEADERS__ = {/* page headers */};  </script></head><body>...</body>

Then in your app you can access the headers via window.__INITIAL_HEADERS__ variable.


You cant get current page headers without sending a http request via javascript.See this answer for more info.

Add a dummy api url on your server and hit it after your page loadn then you can get the headers.

class App extends React.Component{    //some code    componentDidMount(){       fetch(Some_API).then(response=>{           console.log(response.headers)       })    }    //some code}