Skip to content

Instantly share code, notes, and snippets.

@rvenugopal
Last active December 20, 2015 05:49
Show Gist options
  • Save rvenugopal/6081745 to your computer and use it in GitHub Desktop.
Save rvenugopal/6081745 to your computer and use it in GitHub Desktop.
Unable to pass data between chained APIEasy calls. This only works for outgoing headers. However, passing state to modify post request body of subsequent calls does not work. It appears that APIEasy is caching the post data to be sent at initialization and instead of late binding
var APIeasy = require('api-easy'),
assert = require('assert');
///Initialization
var domain = 'localhost',
port = 62601,
clientId = 'MyClientId',
suiteText = 'Chained calls',
customerServiceBase = '/myServiceEndpoint/',
accessToken = null;
var accessTokenUrl = 'access_token?client_id=' + clientId;
var suite = APIeasy.describe(suiteText);
var postData = {
"CustomerID": "15"
};
var getPostData = function() { // Invoked at initialization rather during the post call
console.log('In get post data');
return postData;
};
suite.use(domain, port)
.setHeader('Content-Type', 'application/json')
.before('setAuth', function (outgoing) {
console.log('In suite before fluent' + accessToken);
if (accessToken != null){
console.log('adding auth token');
postData['access_token'] = accessToken;
outgoing.headers['token'] = accessToken;
}
return outgoing;
})
.get(customerServiceBase + accessTokenUrl)
.expect(200)
.expect('should respond with the authorize token', function (err, res, body) {
var result = JSON.parse(body);
assert.isString (result.access_token);
accessToken = result.access_token;
})
.next()
.post(customerServiceBase + 'postCallUrl', getPostData()) //Post data does not contain access token here
.expect(200)
.expect('should respond with valid postCallUrl', function (err, res, body) {
console.log('value of accessToken in postCallUrl:' + accessToken);
var result = JSON.parse(body);
assert.notEqual(result.ErrorMessage, 'invalid access token');
console.log(result);
})
.export(module);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment