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
‘logged-in’ can be any string, its just a session name you want to use
Check Session Status
POST JSON from local file
where tst.json is a file containing json data (this can be piped inline, see below)
GET _rev number for updates/deletes
where 77edfc98e5e33337df9e978f4539ad8a is an _id of the CouchDB document
DELETE Document
with document _id as the first param and _rev id at the end
Verify Deletion
should return 404
GET data by primary index
Get 2 documents after starting after the 3 doc found (_all_docs retrieves all documents)
Search by keys
example with inline JSON argument
The output of the last command as an example of printing JSON to the console and colouring output:
More about Httpie project, installation instructions and all the possible options can be found on the github README of the project itself.