Pass header value for Nodejs API

Asked

Viewed 1,678 times

1

Good personal day I am with following problem, I have an API to be consumed, but it expects a return token validated internally, this token is passed as parameter via header, but I do not use JWT, I take a token generated by the bank. My question is precisely how to send via header this value, to make the API receive. I’ve never done this process, the code I have today is this.

application.get('/listarEmpresas', function(req, res) {

    var request = require('request');

    request('APIURL', function(error, response, body) {

        console.log('error:', error);
        let url = req.url
        console.log('statusCode:', response && response.statusCode);
        console.log('body:', body);
        var arrayJSON = JSON.parse(body);

    });
});

I’d be grateful if anyone could shed some light.

1 answer

1

You can pass the parameters to the function get:

const headers = {
  token: 'TEXTO DO TOKEN',
};

const parametros = {
  url: 'APIURL',
  headers,
};

request.get(parametros)...
  • Good morning Sorack, thank you for that teaching, it worked perfectly!.

  • 1

    @Roberto The answer helped solve the problem and can solve similar questions of other users? If positive do not forget to mark as accepted. To do this just click on the left side of it (below the indicator of up and down votes).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.