Index

REST toolbox for the command-line hacker

I work with lots of REST services those days. RESTful services are easy to access and use because they’re based on well-known tech, this eliminates half of the tedious work. Unfortunatly the other tedious half is still here: interfacing. We still need to get and convert the data from the original format to the format we want. Lately I found two tools that help a great deal with HTTP and JSON: HTTPie and Jq. Today I’ll talk about HTTPie.

I used cURL for almost a decade to deal with HTTP from the command-line, as few month ago I heard about a command line client called HTTPie, that has a nice interface that totally makes sense:

$ http --form POST localhost:8000/handler foo=1 bar=hello

What does it do? It does a HTTP POST on localhost:8000/handler with the following body:

POST /handler HTTP/1.1
Host: localhost:8000
Content-Length: 15
Accept-Encoding: gzip, deflate
Accept: */*
User-Agent: HTTPie/0.8.0
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded; charset=utf-8

foo=1&bar=hello

It’s exactly the kind of stuff I want. I often automate the common stuff away with a function, like this:

http() {
    # 1st parameter is the path, we pop it out of the parameter list
    local urlpath = "$1"; shift

    # since we use http has our function name we have to use `command' to
    # call the executable http and not the function
    command http --form POST "example.com$urlpath" "$*"
}

# Do a POST with the following parameters: foo=1&bar=2
http /test foo=1 bar=2

If you’d rather submit JSON instead of an url-encoded form, replace the --form option with --json.

Give HTTPie a shot next time you want to talk with an HTTP service from the command line: it may take you less time to learn it from scratch than remember how to use cURL.