Your data. Anywhere you go.

New Relic for iOS or Android


Download on the App Store    Android App on Google play


New Relic Insights App for iOS


Download on the App Store


Learn more

Close icon

Extract and reuse token

synthetics-script

#1

Dear All,

I want to extract a token from a 1st http call, then reuse it in a second call
How can I proceed ?

I execute : $http.post(options, callback);
Inside callback I can see the token extracted correctly

How can I do to reuse it the second call ? in options_link
$http.post(options_link, callback_link);

Thanks in advance,
Laurent

Here is what I did :

var formData = {
grant_type: ‘password’,
client_id: ‘XXXXXXXXXXXXXXXXXXXXX’,
client_secret: ‘XXXXXXXXXXXXXXXXXXXXX’,
username: ‘uuid,
password: ‘myped’
};

function sleepFor( sleepDuration ){
var now = new Date().getTime();
while(new Date().getTime() < now + sleepDuration){ /* do nothing */ }
}

sleepFor(2000);

var body_link = “{“Requests”:[{“requestId”:“12313”,“device”:{“codentify”:“132”,“consumerid”:“132123”},“action”:“Link”}]}”;

console.log("---->START<----");

//Import the assert module to validate results.
var assert = require(‘assert’);

var options = {
//Define endpoint URL.
url: “https:// endpoint/token”,
//Define body of POST request.
formData: formData,
//Define insert key and expected data type.
headers: { ‘Content-Type’: ‘application/x-www-form-urlencoded’ }
};

//Define expected results using callback function.
function callback(error, response, body) {
console.log(body);
//Log status code to Synthetics console.
console.log(response.statusCode + " status code")
//Verify endpoint returns 200 (OK) response code.
assert.ok(response.statusCode == 200, ‘Expected 200 OK response’);

//Parse JSON received from Insights into variable.
var info = JSON.parse(body);
//Verify that info contains element named errorCode with a value of 0.
// assert.ok(info.errorCode == 0, ‘Expected errorCode = 0’);
//Log end of script.
// console.log(info.errorCode + " Error code (expected is 0)");
console.log(" access_token=" +info.access_token);
var access_token=info.access_token;

return access_token;
}

//Define expected results using callback function.
function callback_link(error, response, body) {
console.log(body);
//Log status code to Synthetics console.
console.log(response.statusCode + " status code")
//Verify endpoint returns 200 (OK) response code.
assert.ok(response.statusCode == 200, ‘Expected 200 OK response’);
}

var options_link = {
//Define endpoint URL.
url: “https://endpoint/DeviceLinkUnlink”,
//Define body of POST request.
body: body_link,
//Define insert key and expected data type.
headers: { ‘Content-Type’: ‘application/json’, ‘Authorization’: ‘Bearer TOKEN’ }
};

//Make POST request, passing in options and callback.
// token is generated
console.log("---->GET TOKEN<----");

$http.post(options, callback);
console.log("---->NEW TOKEN<---->" + access_token + “<–”);

console.log("---->CALL LINK<----");
sleepFor(5000);
$http.post(options_link, callback_link);
console.log("---->CALL UNLINK<----");


#2

@l.gachon - Synthetics scripts all are run in Node.JS, an asynchronous runtime so your script as written will not execute steps in order. There are multiple different ways to ensure your steps are executed in order. One method would be to have the callback of your first http call make your second http call. An alternative way would be to use the Q promises library to write multiple functions. Here is an example of the second method Sequence steps with Q