cURL Explained: From Basic Server Requests to API Calls

I am Barun Tiwary, learning to scale software to millions of users.
Introduction
cURL (Client URL) is a utility tool built on top of multiple underlying libraries, including libcurl, libcurl-easy, libcurl-share, and more. In this article, we will learn how to use this tool throughout the Software Development journey.
But before that lets understand about the server.
What is a server?
It is just like your personal laptop or computer that serves something. It could be anything from files to web pages. Simple right?
So, what does it look like?
Though there is no face to it, if you want, you can buy your own server as well for just a few hundred rupees.

Here I own 2 different servers.
First server is running the famous n8n automation tool for my social media automation.
And the other server serves the website I own.

This is what a server looks like. Yes, it is the os without any graphics, mostly.
Now, in my second server, I have some web apps. To test those web apps or to interact with them, we can use terminals. But what if we just need to check if the website is up and running, or we need to create some automation that checks every hour if it is up or not?
So, this can be done using cURL.
Now, what exactly is cURL?
It is the tool that helps us to transfer data from or to the server using a URL. And as we know, there are multiple ways we can connect to a server like http, ftp, mqtt and other; cURL supports most of them. Which means we can connect to the server via most of the protocols and transfer or download data through it.
First command.
Now let's make our very first curl request to a server.
curl protocol://resource-path
The above is the very basic way of using the cURL command, where we first tell the terminal to use curl the tool, and then we specify from where to get the resource.

So what actually happens?
The
curlcreated a request with the resource informationMade a request to that resource based on the protocol and flags passed
Collect the response and print it into the
STDINor in simple words, in the terminal.
Now we got to know about request and response. So what are these now?

As you can see above, when you request any website in your browser, it requests that domain just like we did it using curl. When we open that request, we see there is something called Response Headers and Request Headers.
So, in requests, we define multiple things like.
methods
path
root domain
content-type
protocols
and many more things, though, which server understands what we are trying to access.
And just like the request server create response with the same information, in addition with extra information that we request to the server.
API calling.
Now lets move on to how to make api request while testing APIs or for doing reverse engineering.
Before moving ahead, we first need to understand some nuances of the API, like
Methods : There are multiple methods that we can use to make an API request
GET → It is used to request some resource from the server
POST → It is used to create a new resource in the DB through the server.
PUT → It is used to update the existing resources.
Content-Type : What type of content are we requesting from the server, like JSON, form data, or what?
Authorization : It is used to verify our authenticity, which means we tell the server look, I have access to this specific API, here is the key (Access Token). Check this and let me access the resource.
Now lets make some API requests to real servers and see what happens.
Getting Joke from API
curl -X GET -H “Accept: application/json” https://icanhazdadoke.com

As you can see, we have successfully made our 2nd API request using curl.
Explanation
-X GET → X flag defines which method we want to use, like GET, POST, PUT, DELETE, or what.
-H “Accept: application/json” → It defines what type of content we are expecting.
And finally, we added the actual API https://icanhazdadjoke.com where we need to make an API request to get the response.
Let me know if you want a more detailed article around cURL.






