Getting Started with cURL

What is cURL
Curl (client URL) is a command line tool, that is used to send or receive data from a server.
What is a Server ?
A server is a computer or system which provide data, services, resources to other computers, which called as Clients.
Let’s just think that you want to watch a lecture on chaicode.com, So what will you do, you open your browser and type courses.chaicode.com/learn. So here your device is a client and it is talking to the server.
Why we need to Talk to the Server
We talk to servers, because our own device (phone or laptops) are like islands we can’t see what is happening all around the world. Talking to the server connects us to whole world.
Here’s some reasons why we need them :
To share and access data : Servers can hold very large amout of data while your phone can’t so you can access servers data using internet.
To communicate with other people : You cannot talk to your friend around the world easily.
You send a message on whatsapp, and whatsapp send it to its server, then the server finds your friend and delivers the message to him.To do heavy Processing : Some tasks are too big to be handled by your phone or laptop, and server can do big tasks, like finding my article in the whole web.
Curl is a way to talk to the server via command line interface, as there is no user interface and graphics loading accessing websites using curl is very fast.
Why programmers need cURL
Programmers use cURL
Becuase it doesn’t provide unnecessary data, without curl you need to open chrome then its dev tools to see specific response from the server, while when we use curl we get what we wanted.
For testing API’s and backend response, programmers use it to send specific json data or some token to check whether the API is responding correctly or it has bugs to correct, if a website is down it will respond with a error code like 400, 404.
For Scripting : Okay, this is the superpower of curl you can use curl with a code script to automate a process, now for every hour or what you’ve set the responses will be sent to you. Popular use is tracking of price of a product.
Becuase it’s everywhere, curl comes with all the major operating systems built-in, you doesn’t need to install it.
Making a Request using cURL
Type command on your Terminal:
curl https://wwww.google.com
What will happen,
We tell terminal to give us google.com webpage.
But, It give us a wall of text(HTML code).
This is the raw message server sends back.
Browser turns this into webpage but cURL show as it arrived.
How it Looks

Browser request vs cURL request:
| Feature | Web Browser | cURL |
| User Interface (UI) | Browser (Graphical) | Terminal (Command Line) |
| Rendering | Renders HTML, CSS, and Images | Displays raw text(Html code) |
| JavaScript | Executes JS | Does not execute JavaScript |
| Cookies | Automatically manages cookies | Must be manually passed |
| Headers | Sends complex, predefined headers | Sends minimal headers by default |
| Use case | Visual interaction | Testing API or debugging |
Understanding Request and Response
When we use curl we start a two way process between us and the servers, the us can be using a command line interface or a browser.
Request
It is like a question we ask to server.
When we type a url on browser or use
curlcommand, we send a request to the server, this request contain some informationMethod : Get or Post (Requesting data or Sending data)
URL : To find the server we are looking for
Body : If we are sending data using Post request, the actual message goes here.
Response
It is the answer sent by the server.
Once the request is received by the serve it processes it and send us the response accordingly.
A response containg usually 2 things
Status Code : A three digit code
200 : Everything worked correctly
404 : Not found
500 : Server Error
Content : This is what you asked it can be either a HTML of a page or a image or can be a JSON data

Using cURL to talk to APIs
An API (Application Programming Interfaces) is just a server meant for computers to talk. There are two main ways to talk to them: GET and POST
GET
The GET method is used to request data from server.
This method is used when you type URL in browser or click link.
Here is example let’s say,
A GET request is when you ask the kitchen to find you something that already exists.
curl https://api.dinner.com/menu
This request is like the kitchen sends you their menu.
POST
The POST method is used to send data to a server to create or update a resource.
It is commonly used when submitting forms or uploading files.
Here is example let’s say,
A POST request is when you send information to the kitchen to create something new.
curl -X POST https://api.diner.com/order
This request is like hey this is my order cook this for me.
Common mistakes beginners make with cURL
Missing Quotes: Always put your URL in "quotes."
URLs often have special characters like
&or?. If you don't put quotes around your URL, your computer’s command line gets confused and thinks those symbols are instructions
Example:
curl "http://site.com?user=me&pass=123"
- Missing Label: Headers
When you send data, the server needs to know what it is. You always need to mention that otherwise browser will get wrong information.
-H "Content-Type: application/json"
- Forgetting the Request Method:
By default, cURL performs a GET request. If you want to send data via POST, many beginners forget to specify the method.
USE: -X POST or let the -d (data) flag implicitly switch it to POST for you.
- Forgetting Protocol:
Websites often move from http to https. If you cURL the old address, cURL will stop.
USE: Add -L (for Location). It tells cURL, "If they moved, go to the new address automatically."
Final Thoughts
Curl is a very useful tool to test and debug APIs, request and receive responses. It is way fast then sending request using a browser and is mainly used by programmers to save time and view only that they want to see.
Thanks for Reading.

