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

Synthetic API Test. JSON value persistence from an HTTP POST call and used in an HTTP GET call



I am trying to get a JSON value from the body of an HTTP POST call response. I can get the response and set it in the HTTP POST function, but I can not return it to a global variable or object. It is reported as undefined. The value I am after is the auth token. I need to use it to overwrite the header on a following GET API request. These requests need to be chained together as the tokens expire quickly and I need to use it for a production monitor.


Hi, @Ronald.Johnson1: Can you post some code showing what you have tried?


Hi @Ronald.Johnson1 - Have a look at my API Synthetic sample which covers how to retrieve an OAuth token for use in an API call. You could remove the check for maintenance window if it is not required.


Stefan, I am going to try your promises approach so far I can return the Token I need to the next response.

Options sets the Authorization header to a value for the post.
I need to set the JSON body value to the options Authorization header value in the next API HTTP GET call.
This is what I have so far and will try to adapt it for promises.

var assert = require(‘assert’);
var info;
var options = {
//Define endpoint URL.
url: ‘’,
//Define body of POST request.
body: ‘[{“value”:"*"}]’,
//Define insert key and expected data type.
headers: {
‘x-api-key’:‘key here’,
‘Authorization’:‘initial auth here’,
‘origin’: ‘
function callback(error, response, body) {
console.log(response.statusCode + " status code")
//Verify endpoint returns 200 (OK) response code.
assert.ok(response.statusCode == 200, ‘Expected 200 OK response’);
var info = JSON.parse(body);
//Verify that info contains element named success with a value of true.
assert.ok(info.success = String, 'Expected True results in Response Body, result was ’ + info.success);
//Log end of script.
console.log(info.value); // shows token value.
console.log("Success assertion complete, and end of API token test. ");



Hi @Ronald.Johnson1, just wondering if you were successful implementing @stefan_garnham’s promises approach? :slight_smile:


Yes I was, I created an object to hold the return token values.
`let myOptions = {

//Then I added a return statement to extract security token and write it to the myOptions object.

var extractSecurityToken = function(data, myOptions){
console .log(“Extracting Security Token”);
log(data in extract security token is : ${data});
var deferred = Q.defer();
log(‘Parse JSON received as into variable’);
var info = JSON.parse(data);
myOptions = info;
log(“Security token retrieved successfully”);
log(info was assigned JSON.parse(data); and is : ${info});
return myOptions; /returning the key to my object
return deferred.promise;

//Then I add that to a header and clean up the text in a variable myKey in the build api options function.

var buildApiOptions = function(securityToken){
var deferred = Q.defer();
log(“Building API options”);
log(in buildApiOptions myOptions dot value is : ${JSON.stringify(myOptions.value)});
var mykey = Bearer ${JSON.stringify(myOptions.value)};
var newKey = mykey.replace(/"/g, ‘’);
log(Extracted key plus Bearer in build api options are ${mykey});
// build your api options here
var options = {
//Define endpoint URL.
uri: ‘https://api.yourpath/’ + DBVauel+’/headers’,
//log(API options when making the call are: ${JSON.stringify(options)});
//Define body of POST request.
body: ‘bodyText’,
//Define insert key and expected data type.
headers: {
‘x-api-key’:‘your key’,
‘origin’: ‘

//Then I let the promise chain run normally


Thanks for the help. I can now chain API calls as much as i need to.


Fantastic! Thanks for letting us know @Ronald.Johnson1 - And thanks as always for helping out here @stefan_garnham