"What Runs The World? CURL!"
Curl is the powerful command-line tool that takes the communication to another level.
I hear the beat and the lyrics from Beyoncé's song, "Run the world, girls!", echoing in my head while I am writing this post.
After scratching the surface a bit and experimenting with the powerful command-line tool
curl I've almost experienced "The Matrix".
Ok, maybe not the real Matrix, but close enough.
To start with, curl is a command line tool and library used to faciliate the transfer of data. With more than 11 billion (!!) installations it for sure is an impactful tool that enables devices and applications to communicate. Hardware as well as software.
I first heard about
curl when I built my first REST API using Java and Spring.
Behind The Web...
In a previous issue, The Bones, The Body & The Brain we explored three programming languages (yes, you read it right: HTML is a programming language) we use for web applications. These languages are used to setup the structure, styling and behaviour of an application.
But that is only the beginning. A web application cannot be left there on your local machine. It needs to get out and interact with the rest of the world.
So, once we have our application in place, how does it communicate with the rest of the Matr..(sorry) the Internet?
This is where
curl comes in. But before drilling down on it, it's worth understanding the basics on how the web works.
It's worth knowing a bit about HTTP protocols, the concept of API's and a also to understand what the heck JSON is.
HTTP - The Communication Protocol
The web application we have built is based upon media files, stylesheets, html files and more, dependending on how complex we choose to build it.
To retrieve all these layout descriptions, videos and documents we need a standardized protocol that enables the communication. Based upon simple requests and responses.
Without this protocol, our browsers (that we normally use to call various servers around the world) cannot retrieve the necessary data without the HTTP protocol.
The below video explains more about HTTP and what is is all about.
API's Tells Us How To Communicate
Ok, now when we know a bit what enables our web apps to communicate with servers and other applications we can move on to the API's.
The Application Program Interface is like an instruction on how to use and manipulate the data that is "hidden" in the back-end.
Basically a web API is directing how to Create, Read, Update and Delete data entries in your application. It is basically the "Ikea instructions" on how your data can be manipulated.
What About JSON?
Since we are living in a world full of data, created and retrieved based on various languages we need to enable ourselves to send the data that can be interpreted in each specific language.
Some popular API's that we can use JSON in our data exchange:
- Google Maps
- Google Auth 2.0 Authentication
- Facebook Social Graph API
- Spotify Music Web API
- LinkedIn Profile API
For a deeper dive into JSON, checkout below.
So, now when we know a bit about the protocols, web interfaces and data-transfer formats we use to send and receive data between servers and applications, let's go
Invented and maintained by Swedish engineer Daniel Stenberg, it is continuosly developed through open source so if you are eager to contribute, there is a highly active community around this.
In fact, it is so popular, yet discretely used that we can identify its applicability everywhere in daily life.
It is “used in cars, television sets, routers, printers, audio equipment, mobile phones, tablets, media players and is the Internet transfer engine for thousands of software applications in over ten billion installations“, as stated on the official
curl is the powerful command-line tool that takes the communication to another level.
Try it out yourself!
curl from your preferred command line (GitBash, Terminal etc.).
Here are some commonly used commands that we can use and a bit on what they can do for us.
By default, curl makes a get request, as below.
This would simply return a server's response directly into the command shell.
Another example. Say you are building a cryptocurrency trading app. If you built the back-end API first, you could access your exchange rate data before even building a webpage.
Try it yourself!
If we would have to delete a record in a database, we can easily do that (be careful now :) ) by the DELETE command. It needs to be prefixed with -X which is short for request.
Let's say we have a list of resources accessed through a sample-api endpoint. We could delete a record by specifying the id with the below command.
curl -X DELETE http://sample-api.com/sample-resource/id
Let's say we want to add a user to our list. We then have to use -d as our curl command to add 'Lily' to our user list.
curl -d "username=Lily" http://sample-api.com/users
NB! If you are familiar with HTTP verbs, please note that curl defaults to POST method.
So if you want to update Lily's name in the list you need to use her id and change her name accordingly:
curl -X PUT -d "username=Mary" http://sample-api.com/users/1
You have got a little intro to curl and now you can go
curling and exploring the world yourself.
Use curl to test your own applications or retrieve data from external APIs. Although most APIs require an authentication process, there are some open ones that you can start using right away. Here are a few examples that don't require authentication.
Maildrops & Articles
Join my circle and get updates when I write something new. No spam ever.