0

I want to write Basic authorization in the response header and redirect it to the some url, so I do that

    res.setHeader('Authorization', 'Basic a3p0ZXN0YXBpOkthMnFUdVZN')
    res.setHeader('Location', result.data.registrationCreditUrl)
    res.writeHead(302)
    console.log(res)
    res.end('end');

it sends it the url and it says 401 Unauthorized, but if I do get request to that url in the postman and set header manually to (Authorization - Basic a3p0ZXN0YXBpOwqeMnFUdVZN) it works fine

Akezhan
  • 73
  • 6
  • I am not sure, I get the exact question here but if you want to redirect to a particular page you can use `res.redirect('yourpath')` – sagars01 Mar 19 '19 at 05:12
  • @sagars01, but I also want to set header, if I will not do that, the web page I wanna redirect to, returns an error 401 Unauthorized – Akezhan Mar 19 '19 at 05:15
  • Yes you can do this `res.redirect('yourpath'); res.setHeader('Auth' , 'Token');` or vice versa – sagars01 Mar 19 '19 at 05:17
  • @sagars01, No, still 401 here is my res [Object: null prototype] { 'x-powered-by': [ 'X-Powered-By', 'Express' ], authorization: [ 'Authorization', 'Basic a3p04740YXBpOkthMnFUdVZN' ] } } – Akezhan Mar 19 '19 at 05:22
  • @Akezhan When you do res.setHeader you are setting a response header to the client. The 302 header will tell the client to send an identical request to the url in 'location' header. Note, this will be an identical request. Check this out: https://stackoverflow.com/questions/24179361/how-to-pass-data-in-http-header-while-redirecting-a-request-in-java Also, you might want to proxy the original request instead of redirecting – s_a94248 Mar 19 '19 at 06:36
  • you shoud set headers to the req object and not the res object. res.setHeaders() will set response headers. – Surya Mar 19 '19 at 07:04
  • @singh_arpit it does not work, on the postman I send a request to my server with basic auth, and redirected it on my server, still 401 status code – Akezhan Mar 19 '19 at 09:28
  • @Akezhan I am certain that the auth header would be sent to the second server if it was sent to the first. Maybe the token itself was wrong? – s_a94248 Mar 19 '19 at 12:29

0 Answers0