API(Application Programmable Interface)s are the bread and butter of modern day service communications.
- Searching an article on amazon?
- API call.
- Listing the tracks for a given album on spotify?
- API call.
- Updating the news feed on your favorite subreddit?
- Yet another api call...
As developers, we often find ourselves either developing APIs on our own, or stitching different APIs together to create something awesome (which will possibly end up as a new api :ø).
To be able to quickly check the functionality and/or data we receive from a given API we can issue HTTP(or HTTPs, I will just refer to them as HTTP for this article) requests.
It can be quite cumbersome to write HTTP requests in a given language and be able to monitor the results. Therefore graphical apps like Postman or Insomnia were born. While these tools are good at issuing HTTP requests, great, terminal based, lightweight alternatives do exist.
Procedure
- We will forget about graphical applications to issue simple http requests for now and have a look at the power of terminal based ones.
The competitors
CURL - landlord of internet communication
- For quick testing of api related stuff, my go-to tool has been curl. It is a library which is underlying in most of the internets software, and yet easy to use inside the terminal.
VS
HTTPIE - CURLs relative who specialized in HTTP.
- Describing itself as "human-friendly CLI HTTP client for the API era".
- It comes with (in my opinion) more intuitive, user friendly commands, supports colors and does some formatting.
But.. Why would we need all this?
- Curl is fine, dude...
Let's have a look at our scenarios! :]
The Scenarios
- Issuing a simple GET request
- Issuing a POST request with json data inside the body (which is a common way of issuing post requests).
- Issuing a PUT request with an authorization header containing an imaginary token (which is a common way of issuing delete requests) and json data inside the body.
Issuing a simple GET request
- Our api lives over at localhost:3001 and has a /todos route. To get all todos we will have to issue a HTTP request over to http://localhost:3001/todos.
curl http://localhost:3001/todos

http localhost:3001/todos

Issuing a POST request with json data inside the body
- To create a todo we have to send data for the following fields
- title which has to be a string
- completed which has to be boolean (true or false)
curl -d '{ "title": "learn curl post request", "completed": false }' -X POST http://localhost:3001/todos -H "Content-Type:application/json"`

http POST localhost:3001/todos title="feed the duck" completed=false

Issuing a PUT request with an authorization header and JSON data.
- To update a todo we have to authenticate ourselves. In this dummy api we have to pass the header 'authenticated="true"'
- To update the todo, we also have to pass data to update it with.
- We reach the specific todo by UUID(Universally Unique Identifier) under the route http://localhost:3001/todos/valueOfUuidHere
curl -d '{ "title": "learn curl post request", "completed": true }' -X PUT http://localhost:3001/todos/c09e4138-22d0-4dee-bde2-ed3a23a923f4 -H "Content-Type:application/json" -H "authenticated:true"`

http PUT localhost:3001/todos/26eab8fa-2c4f-4777-a5a5-8d2d91fd02c3 title="feed the duck" completed=true authenticated:"true"

Installing HTTPIE
- using python and pip
python -m pip install --upgrade pip wheel python -m pip install httpie
- Don't want to use python? You can follow the instructions for your OS here
Installing curl
- This is installed by default on all sane systems. Otherwise you should be able to figure out how to install by asking your favourite search engine.
Final words
- While I will be going with HTTPIE for simple api queries, it is worth noting that every tool has to be evaluated against the use case.
- I will stick with "use httpie when you can, know CURL exists for specific use cases".
- As always, I have created an example repo which the exact backend I did use. If you want to try it out, you can find it here.
- An interesting comparison by the creator of curl (possibly slightly biased) can be found on his homepage. You can read about it here. I would not consider transporting 80gb over the wire as my daily use case, but situations vary.
- I would like to know the opinions and experiences of the readers(yes, you!) about this, but yet have to implement a commenting function for my blog.. :O