HTTP Debugging with Node and http-console

Share this article

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.

Frequently Asked Questions about HTTP Debugging with Node and HTTP Console

What is HTTP debugging with Node.js and why is it important?

HTTP debugging with Node.js is a process that involves inspecting and modifying HTTP traffic between a client and a server. This is crucial in web development as it allows developers to identify and fix issues related to HTTP requests and responses. By using Node.js for HTTP debugging, developers can leverage its asynchronous, event-driven nature to handle multiple connections efficiently, making it an excellent tool for real-time web applications.

How does HTTP console work in Node.js?

HTTP console in Node.js is a command-line tool that allows developers to send HTTP requests and view responses. It provides a simple and intuitive interface for debugging HTTP traffic. Developers can use various commands to send different types of HTTP requests, inspect headers, and view response bodies. This makes it easier to identify issues and understand how the application communicates with the server.

What are the benefits of using HTTP console for debugging?

HTTP console offers several benefits for debugging. It provides a simple and intuitive interface, making it easy for developers to send HTTP requests and view responses. It also allows developers to inspect headers and response bodies, which can be crucial in identifying issues. Additionally, HTTP console supports various HTTP methods, making it a versatile tool for debugging.

How can I install and use HTTP console in Node.js?

To install HTTP console in Node.js, you can use the npm (Node Package Manager) command: npm install http-console. Once installed, you can start using HTTP console by typing ‘http-console’ in your terminal. You can then use various commands to send HTTP requests, inspect headers, and view response bodies.

What are some common issues that can be identified and fixed using HTTP debugging?

HTTP debugging can help identify a range of issues, including problems with request methods, headers, and response bodies. For example, you might find that a request method is incorrect, causing the server to respond with an error. Or, you might identify issues with the headers, such as missing or incorrect values. By inspecting the response body, you can also identify issues with the data returned by the server.

How does HTTP debugging with Node.js compare to other debugging methods?

HTTP debugging with Node.js offers several advantages over other debugging methods. Its asynchronous, event-driven nature makes it efficient at handling multiple connections, making it ideal for real-time web applications. Additionally, the use of HTTP console provides a simple and intuitive interface for sending HTTP requests and inspecting responses, making it easier to identify and fix issues.

Can I use HTTP console for debugging in other programming languages?

While HTTP console is designed for use with Node.js, the principles of HTTP debugging apply across different programming languages. However, the specific tools and methods used for HTTP debugging may vary depending on the language. It’s always best to use tools and methods that are designed for the specific language you’re working with.

What are some best practices for HTTP debugging with Node.js?

Some best practices for HTTP debugging with Node.js include using the right tools, such as HTTP console, for sending requests and inspecting responses. It’s also important to understand the HTTP protocol, including the different request methods and status codes. Additionally, it’s crucial to inspect both the request and response headers and bodies to identify any issues.

Are there any limitations to HTTP debugging with Node.js?

While HTTP debugging with Node.js offers many benefits, it’s not without its limitations. For example, it may not be as effective for debugging issues related to the application’s internal logic or state. Additionally, while Node.js is efficient at handling multiple connections, it may not be the best choice for applications that require heavy computation.

How can I improve my skills in HTTP debugging with Node.js?

Improving your skills in HTTP debugging with Node.js involves a combination of learning and practice. Start by understanding the basics of the HTTP protocol and how Node.js handles HTTP requests and responses. Then, use tools like HTTP console to practice sending requests and inspecting responses. Additionally, consider working on real-world projects or contributing to open-source projects to gain practical experience.

Ian OxleyIan Oxley
View Author

Ian Oxley has been building stuff on the Web professionally since 2004. He lives and works in Newcastle-upon-Tyne, England, and often attends local user groups and meetups. He's been known to speak at them on occasion too. When he's not in front of a computer Ian can be found playing guitar, and taking photos. But not usually at the same time.

javascriptnode.jsnodejs
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week