I want to get information from an HTTP response header. I do an HTTP request with the code example below.
var token = '123456';
var r = new XMLHttpRequest();
r.open('get', '/api/users/@me');
r.setRequestHeader('authorization', token);
r.send();
In the next step, the server checks if the request header includes a valid authorization token. If the token is valid the server sends a response.
app.get('/api/users/@me', (req, res) => {
if (req.headers.authorization == '123456') {
console.log(true);
res.send('valid token!');
}
});
Now I planned to get the request and display the content("valid token!") of it on the page, but I have no clue how I can do this.
I also tried to do the request like this:
fetch('https://localhost/api/users/@me', {
headers: {
authorization: '123456'
}
}).then(result => {
//...
})
But I don't find a way how to get the content out of it and display it on my page.