Skip to main content

Command Palette

Search for a command to run...

The Curl Command

Updated
5 min read

When you enter the world of terminals via any operating System be it windows, mac or the OG Linux - it feels like you have grown up and hold some superpowers. Even running the ls command and the mkdir command feels like growth - and IT IS. Not because its difficult but because you chose to stop fearing something. And when you grow into it - you will feel it - the hunger for more. And curl is one of those commands that can help you satiate it …. temporarily. And if you are someone who doesn’t use the command line - fear not - because once you do start - you are going to not even want the GUI.

To understand the CURL command lets first clear up a bit about what is HTTP and TCP. It will help us out a little - because I am not going to underestimate you the way you do to yourselves. We will in this article not learn how to use the CURL command but actually the sheer essence of it. And don’t be scared of these puny terms like HTTP and TCP. In fact remember - the fancier the term the more it is evident that the developer who coined it was insecure about his idea being called too simple.

TCP

TCP aka - the transfer control protocol is one of the most important protocols on the internet. Over the internet the data flows in the form of data packets- meaning fixed lengths of data flow at a time.
In that data TCP comes in and adds some of its own data. Think of it like TCP using a label on a parcel . In fact imagine TCP as the amazon delivery service. Amazon will attach a label to it.
Now the delivery people will have bought the packet - but which hose will the packet go to - did it reach the customer etc is determined by TCP. If the packet does not reach and our delivery service comes to know of this - they will ask the stock managers to send in another package. That is how TCP works.

HTTP

To understand HTTP lets try and understand first what is a server.

Server

A server is a remote computer that is used to maintain some kind of features and data management and provide a specific software service that to a client computer. Kind of like a restaurant you can order food from .

Now HTTP has the job to enforce some rules as how to how a server and a client - the person using that server will contact. Some data security - your food should not be stolen on the way - or ensuring it takes the right order - what you wanted from the server in clear words.

The main characteristic is the request response model. And the formats of those requests and response - which too are nothing but data.

Now comes in our CURL command.

The curl command stands for client URL. It is a medium to contact a server from the terminal instead of a browser. What it simply does is use the terminal to send a request to the server and get the response and throw it on your terminal. Well - words don’t explain much. Lets jump right into the terminal.

As you can see - we got a weird response when we did curl example.com . What is this . Well this is nothing but html - a format that is used by browsers to render data. That means we got the raw format of what a browser actually has internally.

But to be honest its kind of boring to me.

So lets add up some spice to it.

The -v flag stands for verbose and shows how it actually got the data from the server.

Lets examine the output.

Host example.com:80 was resolved.

  • IPv6: 2606:4700:83b2:8c7c:3342:0:ccc4:a209

  • IPv4: 104.18.27.120, 104.18.26.120

  • Trying [2606:4700:83b2:8c7c:3342:0:ccc4:a209]:80...

  • Established connection to example.com (2606:4700:83b2:8c7c:3342:0:ccc4:a209 port 80) from 2401:4900:8858:b95f:6fa6:2bbf:4962:9a9e port 33820

First we can see that a DNS query was sent to resolve the domain name example.com to its respective IP address. It got IPv4 and IPv6 address. It established a connection with the IPv6 one on port 80 successfully.

GET / HTTP/1.1 - requesting root source “/“ using HTTP 1.1

Host: example.com - Our Hostname

User-Agent: curl/8.17.0 -Who called - could have been chrome or Firefox if a browser called

Accept: \/* - Accept Anything in any format*

As we can see after that there is a message - request completely sent off. And we see that the server responds with a status of 200 - which means the request is processed successfully.

Then after some more metadata like content length , type date etc - we get our data.

Wanna play more? We have a dedicated domain for that - httpbin.org. Lets wake up our little friend.

Here with this command curl https://httpbin.org/get

We get some more robotic data. And we see this term called handshake. This happens when we try to talk to a server. The server firsts forms connection with the client and exchange some hash keys . Then forms a secure connection between the client and the server ensuring that during this data transfer no one can possibly see what data is actually being carried - its jumbled up for in between attackers.

At the end this website just logs what it saw when we sent in our request.

Now lets make a post request.

curl -v https://httpbin.org/post -d "curl=superpower"

So this is the command we used.

And you can see we got another category - form - with a key value pair.

Which is the same as what we specified.

The -d flag is used to specify data . And if we do pass data it assumes that we are making a post request.

curl -v https://httpbin.org/post -d "curl=superpower&TCP=OP"

As you can see separated by & we can have multiple key value pairs.

We can also specify specific headers.

curl -v -X POST https://httpbin.org/post /

-H "Content-Type:application/json"/

-d "{"name":"parikar","status":"learning"}"

Now this command specifies headers as well as sends data in JSON format. -X flag is used to over-ride default behavior and POST is used to tell what kind of requets we want to make. And here is the output.

We can see the headers now has Content-Type : application/json

earlier it was application/x-www-form-urlencoded by default.

So this was the curl command . You can explore even more or use man curl in the terminal to read more about it.