Skip to content

Instantly share code, notes, and snippets.

@jlafitte
Last active December 16, 2015 02:58
Show Gist options
  • Save jlafitte/5366060 to your computer and use it in GitHub Desktop.
Save jlafitte/5366060 to your computer and use it in GitHub Desktop.
My spas-simple modification to support passing through a header object.
var request = require("request"),
_ = require("underscore")._;
/*
# Simple API Request
*/
exports["request"] = function(params, credentials, cb) {
var reqString = params.url,
first = true;
_.each(params, function(val, key) {
if(key !== 'url' && key !== 'headers') {
reqString += first ? '?' : '&';
reqString += key + '=' + val;
first = false;
}
});
request({url:reqString,headers:params.headers}, function (err, res, body) {
if (!err && res.statusCode == 200) {
cb(null, JSON.parse(body))
} else {
var result;
try {
result = JSON.parse(body);
} catch(e) {
result = {errnum:1, errtxt:"req failed"}
} finally {
cb(result );
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment