API monitor to get employee details after generating access token

Hi,

I am trying to enable an API monitor where the task is to generate access token then use that token to GET employee details. Is this possible to perform through script in API monitor.

Thanks,
Salman

Hi, @Mohd.Salman2: Yes, it is possible. Which part of the task do you need help with?

Hi Philweber,

I need to send GET request for fetching some details with the access token i am able to generate. So i need to pass that access token to the GET request in the script.

i.e from below the response body consist of the access token.

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);
assert.ok(response.statusCode == 200, ‘Expected 200 response’);
}

How to utilize that access token and send another GET request in the same script?

Thanks,
Salman

Hi @Mohd.Salman2 - The post below contains a sample template which will retrieve an access token and then use the token to call a secured API. You can remove the maintenance window functionality as this is now a feature of Synthetics.

2 Likes

Using @stefan_garnham script i have updated to the below script

var info,token;
var assert = require(‘assert’);

var options =
{
uri: ‘*****’,

body: ‘*****’,

headers: {
‘access_key’: ‘’,
‘client_credentials’: '
’,
‘Content-Type’: ‘application/json’
}
}

$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’: token,
‘Authorization’: ‘*****’,
//‘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’);
}
);

Error persists.

Thanks.
Salman

1 Like

I would suggest using my script but actually remove the function to check the maintenance window, the variables associated with it and also the the bottom of the script so that it ends up like this:

buildSecurityOptions()
.then(function(){
  return getSecurityToken)
  .then(extractSecurityToken)
  .then(buildApiOptions)
  .then(makeApiCall)
  .then(validateApiCall);
},
function(message){
    throw new Error(message);
})
.done();

The whole script is based on promises so that the calls listed above call in the correct order. That should work for you.

3 Likes

@Mohd.Salman2 - Let us know if @stefan_garnham’s script gets you the answers you need :smiley:

1 Like

Hi,

The callback in my code is not working. I guess results from my first POST request is not being passed or its not waiting for the access token to be generated to be passed to the second GET request.

Thanks.

Solved it by cascading the second request within the first request’s response due to Async nature. If the second call was not dependent on the first one, it would work in any order.

1 Like

Hey @Mohd.Salman2 - Sorry for the delayed response here (I had some time off last week).

Glad to hear you got this resolved though :smiley:

1 Like

Hi,

I have the same scenario to work with. Need to do 2 post calls
1.Get the access token from first post call.
2. Using the access token (output from 1st), need to hit another post call inorder to get result.

Could you please help how you have cascaded the second request within the first.

Thanks.

Hi, @sunita.das: There is an example in this post:

1 Like

Thanks, It helped… :slight_smile: