Created
December 21, 2010 21:51
-
-
Save isaacs/750678 to your computer and use it in GitHub Desktop.
upload a file, and dump the response into a file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require("fs") | |
, http = require("http") | |
, util = require("sys") // 0.3.x: require("util") | |
, fileToUpload = "/tmp/request" | |
, fileToDownload = "/tmp/response" | |
, server = "example.com" | |
, port = 80 | |
, path = "/upload/target" | |
, method = "PUT" | |
, headers = { "content-type":"text/plain", "accept":"text/html" } | |
, client = http.createClient(port, server) | |
, request | |
fs.stat(fileToUpload, function (er, stat) { | |
if (er) throw er | |
headers["content-length"] = stat.size | |
request = client.request(method, path, headers) | |
util.pump(fs.createReadStream(fileToUpload), request) | |
// 0.3.x: fs.createReadStream(fileToUpload).pipe(request) | |
request.on("response", function (response) { | |
console.log(response.statusCode) | |
console.log(response.headers) | |
util.pump(response, fs.createWriteStream(fileToDownload)) | |
// 0.3.x: response.pipe(fs.createWriteStream(fileToDownload)) | |
}) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require("fs") | |
, http = require("http") | |
, util = require("sys") // 0.3.x: require("util") | |
, fileToUpload = "/tmp/request" | |
, fileToDownload = "/tmp/response" | |
, server = "example.com" | |
, port = 80 | |
, path = "/upload/target" | |
, method = "PUT" | |
, headers = { "content-type":"text/plain", "accept":"text/html" } | |
, client = http.createClient(port, server) | |
, request = client.request(method, path, headers) | |
util.pump(fs.createReadStream(fileToUpload), request) | |
// 0.3.x: fs.createReadStream(fileToUpload).pipe(request) | |
request.on("response", function (response) { | |
console.log(response.statusCode) | |
console.log(response.headers) | |
util.pump(response, fs.createWriteStream(fileToDownload)) | |
// 0.3.x: response.pipe(fs.createWriteStream(fileToDownload)) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment