Easy 'curling' with Httpie

Easy ‘curling’ with Httpie

Sometimes there is a need to quickly test some web service if its responding correctly to the given data, that is especially true when using a new REST service you haven’t worked with before and just want to try it out. Using curl is not always as fun as it could be because of multiple options which need to be set in order to POST for example some JSON content.
HTTPIE project comes to the rescue

HTTPie is a command line HTTP client. Its goal is to make CLI interaction with web services as human-friendly as possible. It provides a simple http command that allows for sending arbitrary HTTP requests using a simple and natural syntax, and displays colorized responses. HTTPie can be used for testing, debugging, and generally interacting with HTTP servers.

Yesterday evening I tried Cloudant.com which is a CouchDB in the cloud with REST api exposed, I will post some examples on how to use Cloudant with the Httpie installed.

The biggest advantage is that you can start a session using httpie and keep that session open so you don’t have to add username and password options to every request you make when using REST service with authentication.

Logging In

http --session logged-in --auth username:password username.cloudant.com/crud/welcome

‘logged-in’ can be any string, its just a session name you want to use

Check Session Status

http --session logged-in username.cloudant.com/crud/welcome

POST JSON from local file

http --session logged-in POST username.cloudant.com/crud/ < tst.json

where tst.json is a file containing json data (this can be piped inline, see below)

GET _rev number for updates/deletes

http --session logged-in HEAD username.cloudant.com/crud/77edfc98e5e33337df9e978f4539ad8a

where 77edfc98e5e33337df9e978f4539ad8a is an _id of the CouchDB document

DELETE Document

http --session logged-in DELETE username.cloudant.com/crud/77edfc98e5e33337df9e978f4539ad8a\?rev\=1-0af5e64fe24d262db237b9f14046f490

with document _id as the first param and _rev id at the end

Verify Deletion

http --session logged-in GET username.cloudant.com/crud/77edfc98e5e33337df9e978f4539ad8a

should return 404

GET data by primary index

http --session logged-in GET https://username.cloudant.com/animaldb/_all_docs?limit=2&amp;skip=3

Get 2 documents after starting after the 3 doc found (_all_docs retrieves all documents)

Search by keys

echo '{"keys":["elephant", "snipe", "panda"]}' | http --session logged-in https://username.cloudant.com/animaldb/_all_docs

example with inline JSON argument

The output of the last command as an example of printing JSON to the console and colouring output:

httpie colouring

More about Httpie project, installation instructions and all the possible options can be found on the github README of the project itself.

Leave a Reply