Skip to content

The String, Issue 5 - "What Runs The World? CURL!"

Curl is the powerful command-line tool that takes the communication to another level.

Jonas Achouri Sihlén
Jonas Achouri Sihlén
5 min read
The String, Issue 5 - "What Runs The World? CURL!"
https://curl.se/

This issue was initially published through The String, on September 23.

I am hearing the beat and lyrics from Beyoncé's song, "Run the world, girls!", echoing in my head while I am writing the introduction to this 5th issue of The String.

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 of The String, The Bones, The Body & The Brain we explored three examples on programming languages we use to build new 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.

Source: MDN webdocs

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.

Source: Spotify For Developers, WEB API

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.

JSON, or JavaScript Object Notation, enables this. It is language independent and is the standard way nowadays that facilitates storing and sending data between programming languages.

Some popular API's that we can use JSON in our data exchange:

For a deeper dive into JSON, checkout below.

Working with JSON - Learn web development | MDN
In this article, we’ve given you a simple guide to using JSON in your programs, including how to create and parse JSON, and how to access data locked inside it. In the next article, we’ll begin looking at object-oriented JavaScript.

Let's Go curling

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 curling.

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, settop boxes, media players and is the Internet transfer engine for thousands of software applications in over ten billion installations“, as stated on the official curl site.

curl is the powerful command-line tool that takes the communication to another level.

Try it out yourself!

Simply run curl from your preferred command line (GitBash, Terminal etc.).

Curl Commands

Here are some commonly used commands that we can use and a bit on what they can do for us.

Get Request

By default, curl makes a get request, as below.

curl http://example.com/abc

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.

curl https://api.coinbase.com/v2/prices/BTC-USD/buy

Try it yourself!

Delete Records

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

Adding Data

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

Wrapping Up

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.


Thanks for reading, until next time!

And by the way, did you know that you can always access previous issues of The String, here?

"Sharing is caring", don't forget to share this issue with a curious friend or a colleague!

The StringProgramming