HTTPie

A CLI, cURL-like tool for humans

by Emiliano Mancuso

This is another entry in our series of technical articles about software development tools and libraries. Today I would like to introduce you to HTTPie, an HTTP client which will help you to put aside cURL, that wonderful — but not so human-friendly — tool.

cURL is an open source command-line tool and library for transferring data with URL syntax, supporting an extensive protocol list. It is widely-used in development and system administration.

cURL is a powerful tool, but its syntax can be very confusing for the occasional user. I’ve used cURL sporadically over the last few years, and each time I began using it, I had to re-google “how to use cURL”. The syntax is not intuitive for me. If you like to use cURL occasionally, HTTPie will help you get started faster, and improve the readability of your code.

Back to HTTPie, it provides a simple http command — the client side of the HTTP/1.1 protocol — which allows us to send HTTP requests using a simple and natural syntax and returns colorized output. The package implements the GET, POST, and HEAD operations of HTTP/1.1.

Installation

OS X

$ brew install httpie

Universal method

This will work on Windows, OS X, Linux, etc.

# Make sure we have an up-to-date version of pip and setuptools: 
$ pip install --upgrade pip setuptools
$ pip install --upgrade httpie

Usage

$ http [flags] [METHOD] URL [ITEM [ITEM]]

GET

$ http example.org

PUT

$ http PUT example.org X-API-Token:123 name=John

POST

$ http --form POST example.org X-API-Token:123 name=John

Adding the — form or -f option, HTTPie ensures that data fields are serialized as, and Content-Type is set to, application/x-www-form-urlencoded; charset=utf-8.

However, my favorite version is to use a file and redirect the input which is great for complex or nested JSON objects:

$ http POST api.example.com/person/1 < person.json

Request Items

Adding headers is as simple as specifying a key/value pair like Name:Value. If you want to add URL parameters, just use name==value, and for data fields field=value, which will be serialized as a JSON object by default.

A common curl command such as

$ curl -i -X PUT httpbin.org/put -d '{"hello":"world"}' \ -H Content-Type:application/json"

Becomes

$ http PUT httpbin.org/put hello=world

This tool does not provide us with anything new, but it enhances and simplifies the syntax we need to use when testing or interacting with HTTP servers. The syntax of the command arguments closely corresponds to the actual HTTP requests sent over the wire. It has the advantage that it’s easy to remember and read.

You should definitely try it and read the full documentation. I bet you will love the Sessions section as much as I did.


Originally published at bits.citrusbyte.com on August 19, 2015.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.