Friday, 12 May 2017

How to set a header on node js, and retrive it in all requests? It is loosing after set it

I'm trying to set a header that will contain the credentials for Authorization...

I'm using adonis, but the method to set the header is even not working, I used basic-auth api, but neither worked:

request.header('Authorization', 'Basic ${base64credentials}')

I'm trying as well with the native nodejs method (I write twice "response" cause Im using adonis, if I write once, it throws an error!):

response.response.setHeader('Authorization', 'Basic ${base64credentials}')

This last method is working, I can retrive the header with Response.Response.getHeader('Authorization') but I cant retive the header In another request!

I made a route that logs the header with console.log(Response.Response.getHeader('Authorization')) and it logs undefinded...

That means the header is getting lost after set the header and end the response

Here's my code... I'm even doing response.response.writeHead()... according to the documentation, it send a response header to the request.

const result = yield request.auth.attempt(request.input('email'), 
request.input('password'))

const base64String = new Buffer(request.input('email') + ':' + 
request.input('password')).toString('base64')

response.response.setHeader('Authorization', 'Basic ${base64String}')

response.response.writeHead(200, {'Authorization': 'Basic ${base64String}'})

console.log(response.response.getHeader('Authorization'));

return response.end("ok");

How can I set the header without it getting lost???



via Angel Chavez Reyes

No comments:

Post a Comment