🤯 New: Coding Assessments Practice your skills on real-world programming challenges

HTTP Debugging with Node and http-console

    Ian Oxley
    Share

    http-console is a Node module that gives you a command-line interface for executing HTTP commands. It’s great for debugging and seeing exactly what is going on with your HTTP requests, regardless of whether they’re made against a web server, web service, or even a database server.

    Installation

    To use http-console you’ll need to have Node installed. If you haven’t got it installed either head over to http://nodejs.org and download the installer for your operating system, or head over to the Node wiki if you’d prefer to install via a package manager.

    Next, install http-console using npm:

    
    $> npm install http-console2 -g
    

    A couple of things to note:

    • We’re actually installing http-console2, and not http-console. http-console2 is a fork of http-console, but includes a fix for a bug caused by require.paths being deprecated in newer versions of Node. It’s published to npm as http-console2, but once installed you still run it as http-console.
    • We’re installing http-console2 with the -g global switch. This means you can call http-console from anywhere, as it’s installed in a location in your $PATH:

      
      $> type http-console
      http-console is /usr/local/bin/http-console
      

    To start using http-console we just pass it the URL and port of whatever it is we want to connect to, and start issuing HTTP commands.

    Speaking HTTP

    Let’s connect to a server and issue some commands. We’ll keep things simple to start with and issue some GET requests to a web server. I’ll assume that, as you’re reading this, you’re a web developer. And, as you’re a web developer, you’ve probably got a web server running on http://localhost. Tell http-console to connect to it by typing in the following:

    $> http-console http://localhost
    > http-console 0.6.1
    > Welcome, enter .help if you're lost.
    > Connecting to localhost on port 80.
    

    Now you’re connected you can start issuing commands. Type in GET / at the prompt:

    http://localhost:80/> GET /
    HTTP/1.1 200 OK
    Server: nginx/1.0.11
    Date: Wed, 04 Jan 2012 08:40:04 GMT
    Content-Type: text/html
    Content-Length: 151
    Last-Modified: Mon, 04 Oct 2004 15:04:06 GMT
    Connection: keep-alive
    Accept-Ranges: bytes
    
    <html>
    <head>
    <title>Welcome to nginx!</title>
    </head>
    <body bgcolor="white" text="black">
    <center><h1>Welcome to nginx!</h1></center>
    </body>
    </html>
    

    We get back the full HTTP response, including the HTTP headers, and the HTML itself. You can exit http-console by typing .q

    Let’s try another command. Recently I wrote about the express web framework for Node, we created a page to display the ten most recent tweets mentioning Sitepoint. I wonder what would happen if we use http-console to query Twitter’s Search API for similar tweets?

    $> http-console http://search.twitter.com
    > http-console 0.6.1
    > Welcome, enter .help if you're lost.
    > Connecting to search.twitter.com on port 80.
    

    Now issue a GET request for /search.json?q=sitepoint&rpp=10:

    http://search.twitter.com:80/> GET /search.json?q=sitepoint&rpp=10
    HTTP/1.1 200 OK
    Cache-Control: max-age=15, must-revalidate, max-age=300
    Expires: Fri, 17 Feb 2012 22:04:02 GMT
    Content-Type: application/json;charset=utf-8
    Content-Length: 7749
    Vary: Accept-Encoding
    Date: Fri, 17 Feb 2012 21:59:02 GMT
    X-Varnish: 2065334673
    Age: 0
    Via: 1.1 varnish
    Server: tfe
    
    {
        page: 1,
        since_id: 0,
        max_id_str: '170628259464216576',
        refresh_url: '?since_id=170628259464216576&q=sitepoint',
        completed_in: 0.107,
        results: [
            {
                to_user_id_str: null,
                to_user_name: null,
                id: 170628259464216580,
                iso_language_code: 'en',
                ...
    

    Again, we get back the HTTP headers, but this time we get the body of the HTTP response as JSON (the full JSON is omitted to save space).

    But we’re not restricted to connecting to web servers and web services with http-console. We can also use it to connect to database servers that offer RESTful API’s, such as CouchDB. (If you don’t have CouchDB installed, the easiest way to get up and running is to clone https://github.com/iriscouch/build-couchdb and following the instructions in README.md).

    Assuming CouchDB is running (if you installed via build-couchdb starting CouchDB should be as simple as running . ~/path/to/build-couchdb/build/env.sh, then couchdb), connect http-console to it like so:

    $> http-console http://127.0.0.1:5984
    > http-console 0.6.1
    > Welcome, enter .help if you're lost.
    > Connecting to 127.0.0.1 on port 5984.
    

    We can then issue commands against the database. Let’s get a list of all the databases:

    http://127.0.0.1:5984/> GET /_all_dbs
    HTTP/1.1 200 OK
    Server: CouchDB/1.1.1 (Erlang OTP/R15B)
    Date: Wed, 04 Jan 2012 08:26:18 GMT
    Content-Type: text/plain;charset=utf-8
    Content-Length: 25
    Cache-Control: must-revalidate
    
    [ '_replicator', '_users' ]
    

    How about creating a new database?

    http://127.0.0.1:5984/> PUT /foodb
    ... 
    HTTP/1.1 201 Created
    Server: CouchDB/1.1.1 (Erlang OTP/R15B)
    Location: http://127.0.0.1/foodb
    Date: Wed, 04 Jan 2012 09:19:05 GMT
    Content-Type: text/plain;charset=utf-8
    Content-Length: 12
    Cache-Control: must-revalidate
    
    { ok: true }
    

    Re-issue the GET /_all_dbs command, and we’ll see our new database listed:

    http://127.0.0.1:5984/> GET /_all_dbs
    HTTP/1.1 200 OK
    Server: CouchDB/1.1.1 (Erlang OTP/R15B)
    Date: Wed, 04 Jan 2012 09:19:18 GMT
    Content-Type: text/plain;charset=utf-8
    Content-Length: 33
    Cache-Control: must-revalidate
    
    [ '_replicator', '_users', 'foodb' ]
    

    Now let’s add a document to the foodb database. We’ll need to set the Content-Type header to application/json, which is easily done by issuing the .j command (to see all available commands type .help at the http-console prompt):

    http://127.0.0.1:5984/> .j
    http://127.0.0.1:5984/> POST /foodb
    ... { "name":"foo", "body":"bar" }
    HTTP/1.1 201 Created
    Server: CouchDB/1.1.1 (Erlang OTP/R15B)
    Location: http://127.0.0.1/foodb/d4a833a173e9d22594b426fd300010a9
    Date: Wed, 04 Jan 2012 09:36:30 GMT
    Content-Type: text/plain;charset=utf-8
    Content-Length: 95
    Cache-Control: must-revalidate
    
    {
        ok: true,
        id: 'd4a833a173e9d22594b426fd300010a9',
        rev: '1-de4f3804f6f3d2d3a393bec924951e5a'
    }
    

    We can issue HEAD requests to get info about documents, DELETE requests to delete documents, and DELETE requests to delete databases:

    http://127.0.0.1:5984/> HEAD /foodb/d4a833a173e9d22594b426fd300010a9
    HTTP/1.1 200 OK
    Server: CouchDB/1.1.1 (Erlang OTP/R15B)
    Etag: "1-de4f3804f6f3d2d3a393bec924951e5a"
    Date: Wed, 04 Jan 2012 09:36:51 GMT
    Content-Type: text/plain;charset=utf-8
    Content-Length: 113
    Cache-Control: must-revalidate
    
    http://127.0.0.1:5984/> DELETE /foodb/d4a833a173e9d22594b426fd300010a9?rev=1-de4f3804f6f3d2d3a393bec924951e5a
    HTTP/1.1 200 OK
    Server: CouchDB/1.1.1 (Erlang OTP/R15B)
    Etag: "2-3ac7397737175948b7a3a6b7e95d2949"
    Date: Wed, 04 Jan 2012 09:40:14 GMT
    Content-Type: text/plain;charset=utf-8
    Content-Length: 95
    Cache-Control: must-revalidate
    
    {
        ok: true,
        id: 'd4a833a173e9d22594b426fd300010a9',
        rev: '2-3ac7397737175948b7a3a6b7e95d2949'
    }
    
    http://127.0.0.1:5984/> DELETE /foodb
    HTTP/1.1 200 OK
    Server: CouchDB/1.1.1 (Erlang OTP/R15B)
    Date: Wed, 04 Jan 2012 09:41:49 GMT
    Content-Type: text/plain;charset=utf-8
    Content-Length: 12
    Cache-Control: must-revalidate
    
    { ok: true }
    

    So that was a quick look at using http-console to make and inspect HTTP requests. We made a simple GET requests to a web server, made an API call to Twitter’s Search API, and issued commands to a CouchDB server. Granted YMMV, but hopefully you’ll find it a useful addition to your web development tool belt.