I am trying to generate employee details by enabling a synthetic API monitor in Newrelic based on NODEJS. I have successfully generated an access token in first POST request but the second one is failing. This works on Postman.
While validating, I am getting following error
Error : "401 UnAuthorized to access the resource."
Below is the code.
var info,token;
var assert = require('assert');
var options =
{
uri: '***',
body: '***',
headers: {
'access_key': '***',
'client_credentials': '***',
'Content-Type': '***'
}
}
$http.post(options,function (err, response, body) {
console.log(response.statusCode);
console.log(body);
info = JSON.parse(body);
token=info.access_token;
console.log("Security token retrieved successfully");
console.log(token);
assert.ok(response.statusCode == 200, 'Expected 200 response');
}
);
var options2 =
{
uri: '***',
headers: {
'access_key': '***',
"Authorization": "Bearer " + token,
//'Content-Type': 'application/json'
}
}
$http.get(options2,function (err, response, body) {
console.log(response.statusCode);
console.log(body);
assert.ok(response.statusCode == 200, 'Expected 200 response');
}
);
Solved it by cascading first call's response with second request due to Async nature.