Getting Started with cURL

Introduction: Talking to Servers
A server is just a computer connected to the internet. Its job is to listen to the requests of the various clients and send responses according to the requests. When a website or app is opened, thes device sends a request to the server. The servers replies with data like HTML, JSON, images, etc. The website works because clients and servers talk to each other. Websites need data like pages, users, posts and apps need data like login, messages, products. All of this data lives on servers, not on our laptop. Client → Sends request and the Server → Sends response.
What is cURL?
cURL is a tool that lets you talk to the servers from the terminal. It sends requests the same way a browser does, but the only difference is browser hides everything behind UI while cURL show everything raw.
Why Do Programmers Use cURL?
cURL is widely used by programmer for various reason, it helps you see the real web, not the polished UI browser version. Some of the reason why cURL is used by programmers are:
It is used to test servers quickly.
It is used to test APIs without writing code.
It is used to understand how requests and responses work.
It is used to debug backend problems.
Making your first request using cURL
curl https://example.com
What happens when the above command is run in the terminal:
You send a GET request to example.com and then the server replies with HTML. cURL prints that HTML in the terminal. It look messy because Browsers can render HTML while cURL just prints the HTML. Nothing is wrong here, it is just how it works. It is simply sending a request to the server and getting a response back.
Introducing GET and POST
Get request
The Get request is used to get the data from a server and it is the default method in cURL.
Example: curl https://api.example.com/users
Means: The client is asking the server for the users.
Post request
The Post request is used to send the data to a server. This method is used for submitting forms, uploading files, adding new users to a server.
Example: curl -X POST https://api.example.com/users
Means: The client is sending or creating data onto the server.
Understanding Request and Response
Request = what you send
Request is what you send including where you send it, the URL and how it was send using the GET method or the POST method.
Response = what you get back
Response is what you get back after a request is sent onto a server. It includes the status code of the response, 200 meaning success while 404 meaning not found. It also includes the body of the website which is HTML and of APIs which is JSON

Using cURL to talk to APIs:
APIs are servers that return JSON as a response when a request is made to it, it does not return HTML like an average server. cURL is perfect for testing APIs. This makes them essential for building modern web and mobile applications, where data needs to be exchanged seamlessly between the client and server. It is widely used by backend developers to check if endpoints of an API work, check the exact responses and it is used to debug issues fast.
Example:
{
"id": 1,
"name": "Ayush"
}
Common mistakes beginners make with cURL:
They forget to add
https://in the URL while using the curl command.They expect browser like output in the terminal.
They get intimidated by the raw HTML and JSON.
They copy paste commands without understanding the intention of using it.
They think cURL is too advanced.




