Skip to content

Instantly share code, notes, and snippets.

@KOBA789
Created August 25, 2010 16:52
Show Gist options
  • Save KOBA789/549844 to your computer and use it in GitHub Desktop.
Save KOBA789/549844 to your computer and use it in GitHub Desktop.
var spawn = require("child_process").spawn;
exports.cURL = function (url, method) {
this.url = url;
this.method = method;
this.query = new Object();
this.param = new Array();
this.access = function (callback) {
var parameter = new Array();
var queryString = new String();
if (this.method == "POST") {
for (var key in this.query) {
parameter.push("-d", key + "=" + this.query[key]);
}
} else {
for (var key in this.query) {
queryString += "&" + key + "=" + this.query[key];
}
if (queryString) {
queryString = "?" + queryString.substring(1);
}
}
parameter.push(this.url + queryString);
var curl = spawn("curl", parameter);
var stdout = new String(),
stderr = new String();
curl.stdout.on("data", function (data) {
stdout += data;
});
curl.stderr.on("data", function (data) {
stderr += data;
});
curl.on("exit", function (status) {
callback(stdout, stderr, status);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment