Awesome cli tools: HTTPie http client
We frequently find ourselves making http calls from the terminal: for testing out an external API we’re integrating into our projects, or for testing APIs we build locally and in many other cases, too.
In the past, when I wanted to test out an API I was building locally, I would either have to use a rest client with a graphical interface (e.g. Insomnia or one of the several chrome or firefox addons/extensions) or cURL.
While cURL (check out the official tutorial) is powerful and you can do lots of things with it, it is quite verbose in several cases. This is where HTTPie comes in. HTTPie is a user-friendly command-line HTTP client.
Regarding installation, if you are working on linux, httpie is probably available via your package manager. For Windows and macOS, you can find more information in the official documentation.
There are several great things about HTTPie. First of all, its pretty output out of the box. While with cURL, I had to pass its results through a tool like jq (check out my blog post on jq) to pretty print json results, for example. HTTPie colorizes and formats the terminal output by default. Yay!
Below is an example from calling an endpoint of the Star wars API (you can check the website at https://swapi.dev/) by running http https://swapi.dev/api/people/1/
:
There is no straightforward way to get the same output using cURL. You can get something similar by running curl https://swapi.dev/api/people/1/ | python -m json.tool
, but this just formats the output, not colorizing it. Another way is to install jq and run curl https://swapi.dev/api/people/1/ | jq .
.
In both cases, though, we’re missing the information about the response (headers etc). With cURL, if we want to get information about the response, we could do curl -i https://swapi.dev/api/people/1/
, but in this case we cannot pipe the output to a json formatter.
Apart from the syntax highlighting and pretty formatting of HTTPie, another advantage of it is its expressive syntax. Let’s say you want to send a POST request and pass some data to it. Let’s assume that you have an endpoint of an API you’re building, that creates a user: http://localhost:5000/users.
Using cURL, you could hit this endpoints by doing this:
To do the same thing in HTTPie:
How awesome is that? HTTPie’s sensible defaults make it really easy to work with it while you’re building APIs.
We have just scratched the surface here, but there are lots of great features that HTTPie offers: URL shortcuts for localhost (to save a bit of typing :) ), automatic addition of headers for json (content type and accept), getting request data from a file and many more.
Check out the documentation to find out more!
That’s all for now!