Getting Started with cURL
What is Server ?
Server can be as simple as the basic machine which serves the requests and provides the request data to the user. In the Web development or any other tech alos , we use the machine hosted on some CSP (cloud service provider) like Linode, DO , Azure , AWS , GCP etc. which give us the public IP through which we can use the machine directly using the public IP.
we can us the SSH protocol to securely login to the machine directly from the terminal. SSH use the Public-key cryptography , in this technique the pair of keys (public and private key is generated) and the public key is stored on the hosted machine and then we store the private key into our local machine .
CURL
Curl is the basic command line utility used to transfer the data between a system and a server over the network.
Supports multiple protocols such as HTTP, HTTPS, FTP, and SCP.
Used to download, upload, and send data from the terminal
Helpful for testing REST APIs and web services
Works with headers, authentication, and data formats like JSON
Programmers use the Curl because it can be very useful to make the api call without any Client side UI. we can interact with the ser ver api just through the terminal.
Fetching data using Curl command
Just use curl like this with any url
curl https://chaicode.com

Curl with -i flag
curl -i https://chaicode.com
whenever we use -i flag with the curl command then this utility includes the header with the response.
The header icontains the information like Status code for the request , content-type : text/html ; which indicates that the response contain the html which is handled by the browser differently as compared to normal text or json type response.

Understanding Request and Response with curl
Every CURL command follow this flow :-
Request (what you send) :
It Includes the Method type (Get , Post , Patch etc.) , URL , Headers etc.
Response (What You receive) :
It Inclues the status code (which thelp us to find out the success or failure of that request),Headers with the response type related info , and the response body.
Curl with the GET and POST methods
- With GET : Asking for data.
curl https://api.example.com/posts/1
APIs usually return JSON not the HTML.
{
"userId": 1,
"id": 1,
"title": "sample title",
"body": "sample body"
}
- With POST : Sending the data to the server.
curl -X POST https://api.example.com/data \
-H "Content-Type: application/json" \
-d 'name=Mukul&age=22'
Common Mistakes Beginners Make with cURL
Forgetting the HTTP Method —> ALways specify the HTTP method with the curl expect GET .By default, Curl uses GET Method.
Invalid flags —> using some invalid flags with the curl that doesn’t even exists.
Syntax Issue —> Beginners needs some time to get familiar with the Syntax.



