4

I have built a API using node.js and express. But i need to be able to proxy some requests on a specific route to a external server and show the response from the external server to the clint doing the request.

But i also need to forward the basic auth that the client is send along with the request.

I have tried using the request module like:

app.get('/c/users/', function(req,res) {
  //modify the url in any way you want
  var newurl = 'https://www.external.com' 
  request(newurl).pipe(res),

})

But it seems to not send the basic auth header because i get "403 Forbidden" back form the external server(www.external.com)

The request im making is looking like:

GET http://example.se:4000/c/users/ HTTP/1.1
Accept-Encoding: gzip,deflate
X-version: 1
Authorization: Basic bmR4ZHpzNWZweWFpdjdxfG1vcmV1c2*******=
Accept: application/json
Host: example.se:4000
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

And it works if i do the exact same request but against www.external.com directly so there is some issue when doing the proxy in node.

2 Answers 2

2

The request module is totally unaware of anything that you don't pass explicitly to it. To set the request headers and copy the response headers as well do the following:

// copy headers, except host header
var headers = {}
for (var key in req.headers) {
  if (req.headers.hasOwnProperty(key)) {
    headers[key] = req.get(key)
  }
}
headers['host'] = 'final-host'

var newurl = 'http://final-host/...'
request.get({url:newurl, headers: headers }, function (error, response, body) {
  // debug response headers
  console.log(response.headers)
  // debug response body
  console.log(body)

  // copy response headers
  for (var key in response.headers) {
    if (response.headers.hasOwnProperty(key)) {
      res.setHeader(key, response.headers[key])
    }
  }
  res.send(response.statusCode, body)
})
Sign up to request clarification or add additional context in comments.

5 Comments

Ok so from my understanding i should replace request(newurl).pipe(res), with: .get({url:newurl, headers{'Authorization': req.get('Authorization') }}).pipe(res); But i than get: request.get({url:newurl, headers{'Authorization': req.get('Authorization') } ^ SyntaxError: Unexpected token {
Aaa should have seen that. hmm still dont get to work as i get 403 Forbidden. Can i get a log of what is sent? When i do return console.log(res); i get a lot of things that i dont really understand.
If you want to see the output instead of pipe-ing the result you can put a callback at the end of the first method call: request.get({...}, function (error, response, body) { console.log(body); res.send(response.statusCode, body) }. See the documentation of the request module.
Is it possible to forward all headers without setting them manually like request.get({url:newurl, headers: req.headers}).pipe(res); But than overwrite the host header? Because if i pass the host header from request i get a error.
could you please give advice for stackoverflow.com/questions/22684644/… I will appreciate it very much :)
0

Try explicitly passing in the auth details like so

request(newurl).auth(username, password).pipe(res);

https://github.com/mikeal/request#http-authentication

3 Comments

But i only get the auth code encoded in the header from the client request like: Authorization: Basic bmR4ZHpzNWZweWFpdjdxfG1vcmV1c2*******= So i cant just add username/password. I need to forward this header somehow.
You can set the header like so ` request({ url: newurl, headers: {'Authorization': 'Basic bmR4ZHpzNWZweWFpdjdxfG1vcmV1c2*******='}}).pipe(res);`
Sorry im not following you now im not so experienced witd node and express. I guess i need to than get the header from the request and than pass it to the outgoing request to the external server?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.