How to get http headers in React.js

I made a request to the React page and I need to get the headers from the request.

I am calling the page via the url:

http: // localhost / dashboard

and I set the headers like authcode = 1234.

Then I load the page along this route:

<Route path="dashboard" name="dashboard" component={Dashboard} onEnter={requireAuth}></Route> 

is there something like this.props.header, can I call the page constructor?

+5
source share
2 answers

You cannot receive current page headers without sending an HTTP request through javascript. See This Answer for more information.

Add a dummy api url to your server and click it after the page loads, 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 } 
+2
source

Unable to access page headers through client-side JavaScript. You can get these request headers on your server side and then pass them to the index.html your React application. For instance:

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

Then in your application, you can access the headers through the window.__INITIAL_HEADERS__ .

+2
source

Source: https://habr.com/ru/post/1268527/


All Articles