Node: HTTP, HTTPS

10. Node: HTTP, HTTPS

10.2 HTTP client

There is also a HTTP client API, which allows you to make HTTP requests and read content from other websites.

HTTP client

Methods

Client request
Methods

Events

Client response
Methods

Events

Properties

10.2.1 Issuing a simple GET request

http.get(options, callback) A convinience method to make HTTP GET requests.

http.get() returns a http.ClientRequest object, which is a Writable Stream.

The callback passed to http.get() will receive a http.ClientResponse object when the request is made. The ClientResponse is a Readable Stream.

To send a simple GET request, you can use http.get. You need to set the following options:

  • host: the domain or IP address of the server
  • port: the port (e.g. 80 for HTTP)
  • path: the request path, including the query string (e.g. ‘index.html?page=12’)

To read the response data, you should attach a callback to the ‘data’ and ‘end’ events of the returned object. You will most likely want to store the data somewhere from the ‘data’ events, then process it as a whole on the ‘end’ event.

The following code issues a GET request to http://www.google.com/, reads the ‘data’ and ‘end’ events and outputs to the console.

var http =require('http');var options ={
    host:'www.google.com',
    port:80,
    path:'/'};var req = http.get(options,function(response){// handle the responsevar res_data ='';
  response.on('data',function(chunk){
    res_data += chunk;});
  response.on('end',function(){
    console.log(res_data);});});
req.on('error',function(e){
  console.log("Got error: "+ e.message);});

To add GET query parameters from an object, use the querystring module:

var qs =require('querystring');var options ={
    host:'www.google.com',
    port:80,
    path:'/'+'?'+qs.stringify({q:'hello world'})};// .. as in previous example

As you can see above, GET parameters are sent as a part of the request path.

10.2.2 Issuing POST, DELETE and other methods

http.request(options, callback) Issue an HTTP request. Host, port and path are specified in the options object parameter. Calls the callback with an http.ClientRequest object with the new request.

To issue POST, DELETE or other requests, you need to use http.request and set the method in the options explicitly:

var opts ={
    host:'www.google.com',
    port:80,
    method:'POST'
    path:'/',
    headers:{}};

To send the data along with the POST request, call req.write() with the data you want to send along with the request before calling req.end(). To ensure that the receiving server can decode the POST data, you should also set the content-type.

There are two common encodings used to encode POST request data: application/x-www-form-urlencoded

// POST encoding
opts.headers['Content-Type']='application/x-www-form-urlencoded';
req.data = qs.stringify(req.data);
opts.headers['Content-Length']= req.data.length;

and application/json:

// JSON encoding
opts.headers['Content-Type']='application/json';
req.data = JSON.stringify(req.data);
opts.headers['Content-Length']= req.data.length;

Making a request is very similar to making a GET request:

var req = http.request(opts,function(response){
  response.on('data',function(chunk){
    res_data += chunk;});
  response.on('end',function(){
    callback(res_data);});});
req.on('error',function(e){
  console.log("Got error: "+ e.message);});// write the dataif(opts.method !='GET'){
  req.write(req.data);}
req.end();
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s