Making http requests in node (posted 05 Sep 2010)
A few months ago, I set out to add a url shortener to my IRC bot, and ran into lotsa snags. I just could not get the thing to work! Anyways, I finally revisited it and got it to do what I want.
Here’s the gist of me requesting a shortened url for google.com:
Now I’ll do my best to walk through it:
###lines 4 and 5:
First, we require everything we need. In this case, it’s just the http and the querystring modules. The http module is what it sounds like; I’ll cross the querystring bridge when I get to it.
We create the client on port 80.
Now, we make the request. The first argument is the type of request we’re making—in this case, “POST.” The second is the url after the base. Note that it includes the question-mark query mumbo-jumbo! The third argument is http header stuff. Different services will require different things in the header in order to not shit themselves, but just about any service will at least need the host. Why the service doesn’t know who they are, I don’t know. ;)
Now, notice this commented section. This was my original instinct, based on the docs, but for the sort of things that are normally questionmarked, it’s not what you want to do. I think this is more where “POST”-ish information goes, but I wouldn’t know. I’m no http master!
This next bit here is just a series of EventEmitter event handlers. Request emits a response, which contains status code and header info, and has a ‘data’ event, which contains what we’re ACTUALLY interested in.
You have to close the request before you launch it. As the argument, you can write some parting stuff if you want, not that it helps us any. ;)
Hopefully that was helpful!