learnyounode Lesson 7 – HTTP Client

(Last Updated On: 2020-06-16)

For this lesson we need to write a program that preforms an HTTP GET request to a URL, and writes the response data to the console.  GET requests are one of the request methods allowed in Hypertext Transfer Protocol (HTTP).  GET request only retrieve data from the specified resource (determined by the URL the request is used on).  GET requests are considered safe operations that do not change the state of the server the request is sent to because it only retrieves information.

The node HTTP module has a number of methods for HTTP requests including get().  A GET request converted to a string will return an HTML document from the server where the request is sent.  Take a look at the solution below to see how this is done with node.

var http = require('http')
var url = process.argv[2]

http.get(url, function (response) {
  response.on('data', function (data) {
    console.log(data.toString());
  })
})

In the solution above, we are logging the response from http.get() to the console by passing the variable url and a callback function to the http.get() method.  In the callback, we are calling the response.on() method on data, and then passing the data to another callback to log it to the console.

Official Solution

var http = require('http')

http.get(process.argv[2], function (response) {
  response.setEncoding('utf8')
  response.on('data', console.log)
  response.on('error', console.error)
}).on('error', console.error)  

The official solution does things a little bit differently.  Instead of converting the response to http.get() with the toString() method, the setEncoding() method is used to set the response encoding to utf8.  Additionally, the on() method is used to log the data or errors form the response to the console.

My Solution

The solutions above will work fine.  The official solution is arguably best because it also checks for and logs errors on the response function.  However, one thing the official solution will not do is return a response for https GET requests.  Making https GET requests is just as easy as making an http GET request.  See the solution below to see how this is accomplished.

var http = require('http')
var https = require('https')

var url = process.argv[2]
var prefix = url.substring(0,8)

if (prefix === 'https://'){
  https.get(url, function (response) {
    response.on('data', function (data) {
      console.log(data.toString());
    })
  })
} else {
  http.get(url, function (response) {
    response.on('data', function (data) {
      console.log(data.toString());
    })
  })
}

In the example above we assigning a substring() of the url variable to the prefix variable.  The substring the same length as ‘https://’  which allows us to check our url to see if the prefix of the url equal to ‘https://’.  If it is, we pass it to the https.get() method.  If it is not, we pass it to the http.get() method instead.  The alternate solution above could be improved by also checking for errors in the response like the official solution.

Lesson 8

Contents