HomerPlata
HomerPlata

Reputation: 1787

Specifically add browser to POST request in Node.js

I have a proxy endpoint in a Node.js service that is forwarding POST requests as follows:

request.post(
    fullURL,
    { form: req.body },
    function (error, response, body) {
        if (response){
            res.status(response.statusCode).send(body);
        }
        else {
            res.status(500).send("ERROR");
        }
    }
);

And this is forwarding to a Spring Boot service that is attempting to extract the browser info via:

String browser = request.getHeader("User-Agent");

But this is always empty when being forwarded through the proxy. So how can I specifically set User-Agent in the request?

NOTE: req.headers['user-agent'] from the original incoming request is all present and correct, and ready to be injected into forwarding POST request

Upvotes: 0

Views: 373

Answers (1)

Muhan Alim
Muhan Alim

Reputation: 489

From what I can see, your only issue is that you're not actually relaying the user-agent header, but you are reading it. Pass the header object with your POST request like so:

headers: { 'User-Agent': userAgentVariable }

Haven't tested this myself, but try the following:

request.post(
    fullURL,
    { 
      form: req.body,
      headers: {
            'User-Agent': request.getHeader("User-Agent")
      }
    },
    function (error, response, body) {
        if (response){
            res.status(response.statusCode).send(body);
        }
        else {
            res.status(500).send("ERROR");
        }
    }
);

Upvotes: 3

Related Questions