Skip to content

Instantly share code, notes, and snippets.

@zbialecki
Last active January 11, 2016 16:39
Show Gist options
  • Save zbialecki/b27173a218042cd5ed5f to your computer and use it in GitHub Desktop.
Save zbialecki/b27173a218042cd5ed5f to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
var fs = require('fs');
var minimist = require('minimist');
var request = require('request');
var args = minimist(process.argv.slice(2));
var fileName = args.f || 'data.json';
// Load data from file
var testData = JSON.parse(fs.readFileSync(fileName, 'utf8'));
function buildOptions(url, body, headers) {
headers = typeof(headers) !== 'undefined' ? headers : { 'User-Agent': 'test-create-script' };
return {
url: url,
method: 'POST',
port: 80,
json: true,
body: body,
headers: headers
};
}
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
var responseBody = JSON.parse(body.body);
console.log(responseBody);
process.exit(0);
} else {
console.log(response.statusCode + ' Error. Could not complete request.');
process.exit(1);
}
}
var options;
for (var i = 0; i < testData.length; i++) {
options = buildOptions(url, testData[i]);
request(options, callback);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment