Skip to content

Instantly share code, notes, and snippets.

@isaacs
Created December 1, 2010 08:35
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save isaacs/723163 to your computer and use it in GitHub Desktop.
Save isaacs/723163 to your computer and use it in GitHub Desktop.
a simple example of a nodejs http client making a POST request
var http = require("http")
, util = require("sys") // 0.3.x: require("util")
, fs = require("fs")
, client = http.createClient(80, "example.com")
, request = client.request("POST", "/", {"host":"example.com"})
// send body chunks
request.write("hello, world")
// pump a file through
// You'd normally have to call request.end() to actually
// have it send out, but the pump will .end() when the file is done.
// 0.3.x: fs.createReadStream(__filename).pipe(request)
util.pump(fs.createReadStream(__filename), request)
request.on("response", function (response) {
// got a response
console.log("response: "+response.statusCode)
console.log(util.inspect(response.headers))
// read the body
// could listen to "data" and "end" manually, or just pump somewhere
// pumping *into* stdout is kinda lame, because it closes it at the end.
util.pump(response, process.stdout)
// this is how to buffer it. not usually the right thing to do.
// note that an array of buffers is used rather than one big string,
// so that we don't get bitten by multibyte chars on the boundaries,
// or take the performance hit of copying the data to/from v8's heap twice.
// (once to put it into the string, then to get it out later)
var bodyParts = []
, bytes = 0
response.on("data", function (c) {
bodyParts.push(c)
bytes += c.length
})
response.on("end", function () {
// flatten into one big buffer
// it'd be cooler if fs.writeFile et al could take an array
// of buffers and use writev, but alas, not at this time.
console.error(bytes, typeof bytes)
var body = new Buffer(bytes)
, copied = 0
bodyParts.forEach(function (b) {
b.copy(body, copied, 0)
copied += b.length
})
fs.writeFile("the-response", body, function (er) {
if (er) {
console.error(er.stack || er.message)
return process.exit(1)
}
console.error("ok")
})
})
})
@mranney
Copy link

mranney commented Dec 1, 2010

Why not just pump/pipe the response body to a file stream? Otherwise, the entire response has to fit in memory. Also: more streams.

@isaacs
Copy link
Author

isaacs commented Dec 1, 2010

Indeed. That's why the comment says "this is almost always a bad idea". However, if you wanted to JSON.parse it or something, then you have to wait until you get the whole thing.

@ranm8
Copy link

ranm8 commented Jul 18, 2013

Can also be done using Requestify, which simplifies the way HTTP requests are made using nodeJS + it supports caching.

requestify.post('http://example.com', {
        hello: 'world'
    })
    .then(function(response) {
        // Get the response body (JSON parsed or jQuery object for XMLs)
        response.getBody();
    });

@AnkitGujjar
Copy link

i have an html form when it post the value how can we handle the post request and how can we send it to dynamoDb......pls suggest with an example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment