Skip to content

Instantly share code, notes, and snippets.

@mikeal
Forked from isaacs/making-a-put.js
Created March 10, 2010 21:55
Show Gist options
  • Save mikeal/328472 to your computer and use it in GitHub Desktop.
Save mikeal/328472 to your computer and use it in GitHub Desktop.
var client = http.createClient(u.port || (u.protocol === "https:" ? 443 : 80), u.hostname);
var request = client.request("PUT", u.pathname, headers);
request.write(what, "utf8");
request.addListener("response", function (response) {
if (response.statusCode !== 200) return cb(new Error(
"Status code " + response.statusCode + " from PUT "+where));
var data = "";
response
.setBodyEncoding("utf8")
.addListener("data", function (chunk) { data += chunk })
.addListener("end", function () {
try {
data = JSON.parse(data);
} catch (ex) {
return cb(ex);
}
if (data.error) return cb(new Error(data.error));
cb(null, data);
});
});
request.close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment