Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send a POST request with data and with bearer authorization in node.js?

I have successfully been able to call an API using python requests library. Need to make the same request in node.js. Saw various Q&As on SO and tried hard with the request library in node.js but not able to make this work esp. with data and bearer authorization.


1 Answers

var request = require('request');
data = {
    "Inputs": {
            "input1":
            [
                {
                        'Col1': "5",   
                        'Col2': "3.5",   
                        'Col3': "1.5",   
                        'Col4': "0.2",   
                        'Col5': "doesnotmatter",   
                }
            ],
    },
"GlobalParameters":  {
}
}


var options = {
  method: 'POST',
  body: data,
  json: true,
  url: 'https://api.github.com/repos/request/request',
  headers: {
    'Authorization':'Bearer xxxx'
  }
};

function callback(error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body)
  }
}
//call the request

request(options, callback);

For more refference you can go to request library repo.headers

like image 107
zabusa Avatar answered Oct 27 '25 15:10

zabusa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!