Introducing ‘htty’, the HTTP TTY

I’ve been hacking instead of writing during my spare time the last couple months. The result is a streamlined console application for exploring web services and web sites. It’s something of a cross between curl and the Lynx browser.

Installation

It couldn’t be much easier.

You’ll need Ruby and RubyGems. It’s known to work well under OS X against Ruby v1.8.7 and v1.9.2.

Features

  • Intuitive commands and command aliases
  • Support for familiar HTTP methods GET, POST, PUT, and DELETE, as well as HEAD, OPTIONS and TRACE
  • Automatic URL-encoding of query-string parameters and URL fragments
  • Transcripts, both verbose and summary
  • Dead-simple cookie handling and redirect following
  • Built-in help

The things you can do with htty are:

  • Build a request — you can tweak the address, headers, cookies, and body at will
  • Send the request to the server — after the request is sent, it remains unchanged in your session history
  • Inspect the server’s response — you can look at the status, headers, cookies, and body in various ways
  • Review history — a normal and a verbose transcript of your session are available at all times (destroyed when you quit htty)
  • Reuse previous requests — you can refer to prior requests and copy them

Examples

Here are a few annotated htty session transcripts to get you started.

Querying a web service

This simple example shows how to explore read-only web services with htty.

ESVAPI.org example #1

You can point htty at a complete or partial web URL. If you don’t supply a URL, http://0.0.0.0/ (port 80) will be used. You can vary the protocol scheme, userinfo, host, port, path, query string, and fragment as you wish.

The htty shell prompt shows the address of the current request.

The get command is one of seven HTTP request methods supported. A concise summary of the response is shown when you issue a request.

You can follow redirects using the follow command.

ESVAPI.org example #2

You can tweak segments of the address at will. Here we are navigating the site’s path hierarchy, which you can do with relative as well as absolute pathspecs.

ESVAPI.org example #3

Here we add query-string parameters. Notice that characters that require URL encoding are automatically URL-encoded (unless they are part of a URL-encoded expression).

The headers-response and body-response commands reveal the details of a response.

ESVAPI.org example #4

There was some cruft in the web service’s response (a horizontal line, a passage reference, verse numbers, a copyright stamp, and line breaks). We eliminate it by using API options provided by the web service we’re talking to.

We do a Julia Child maneuver and use the address command to change the entire URL, rather than add individual query-string parameters one by one.

Exit your session at any time by typing quit.

Working with cookies

The next example demonstrates htty's cookies features, as well as how to review and revisit past requests.

Google example #1

Notice that when cookies are offered in a response, a bold asterisk (it looks like a cookie) appears in the response summary. The same cookie symbol appears next to the Set-Cookie header when you display response headers.

Google example #2

The cookies-use command copies cookies out of the response into the next request. The cookie symbol appears next to the Cookie header when you display request headers.

Google example #3

An abbreviated history is available through the history command. Information about requests in the history includes request method, URL, number of headers (and a cookie symbol, if cookies were sent), and the size of the body. Information about responses in the history includes response code, number of headers (and a cookie symbol, if cookies were received), and the size of the body.

Note that history contains only numbered HTTP request and response pairs, not a record of all the commands you enter.

The reuse command makes a copy of the headers and body of an earlier request for you to build on.

Understanding complex HTTP conversations at a glance using history

Assume that we have the following Sinatra application listening on Sinatra’s default port, 4567.

This application expects GET and POST requests and responds in various contrived ways.

Sinatra example #1

Here you can see a request body being specified. Type body-set to enter body data, and terminate it by typing Return three times consecutively.

Also note how different response codes are rendered:

  • Response codes between 200 and 299 appear black on green to indicate success
  • Response codes between 300 and 399 appear white on blue to indicate redirection
  • Response codes between 400 and 499 appear white on red to indicate failure
  • Response codes between 500 and 599 appear flashing black on yellow to indicate a server error

Sinatra example #2

As with the abbreviated history demonstrated earlier, verbose history shows a numbered list of requests and the responses they elicited. All information exchanged between client and server is shown.

Getting help

You can learn how to use htty commands from within htty.

Sinatra example #2

The help command takes an optional argument of the abbreviated or full name of a command.

Coming soon

Here are some features that are coming down the pike.

HTTP Secure URLs

The HTTPS code is broken right now. Pardon the dust.

Commands for streamlining web form submission

These features will make htty better at screen-scraping.

Using any of the forthcoming form commands will clear any non-form content in the body of the request. Adding at least one URL-encoded form parameter to the request will set the Content-Type header to application/x-www-form-urlencoded. Removing all URL-encoded form parameters will remove this header.

  • form — display all form parameters offered in the response
  • form-fill — prompt in turn for a value for each of the form inputs in the response
  • form-add name value — add a URL-encoded form parameter for the request, using the specified name and value
  • form-remove name — remove a URL-encoded form parameter from the request, using the specified name
  • form-remove-all — remove all URL-encoded form parameters from the request

You will also be able to pop open a browser window containing request and response bodies.

Shiny curses goodness

We’ll have command history using the arrow keys, command autocompletion, and Tab key navigation of forms.

Custom command aliases and shell emulation of http-console

You should be able to make your own command aliases.

http-console has a nice command-line. We should have an http-console skin for htty.

Contributing

Your patches are welcome, and you will receive attribution for good stuff.

Fork the official htty repository located at http://github.com/htty/htty and send a pull request to htty.

News and information

Stay in touch with the htty project by following the Twitter account: get_htty.

You can also get help in the #htty channel on Freenode. end of article

  1. fooyeahcode reblogged this from njonsson
  2. seapomeranian reblogged this from njonsson and added:
    I’ve been hacking instead of writing during my spare time the last couple months. The result is a streamlined console...
  3. njonsson posted this

Blog comments powered by Disqus
Follow Me on GitHub