Getting started with cURL
Begin Your cURL Journey
What is cURL?
cURL → Client URL → It is a open source CLI (Command Line Interface) which is used for transferring data or to form using URL syntax. cURL supports several different protocols like HTTP and HTTPS and run almost on every platform.
Why programmer need cURL?
cURL = a tool to send requests to servers and see their responses.
Various Platform Availability: cURL easily works on window, MacOS, Linux. It has built in availability in various system. Hence it gives wide range of usage.
Data Transfer: cURL also supports various protocols like
HTTP, HTTPs, FTP, UDP.It has a feature of testing endpoints and sending request
(GET, POST, PUT, DELETE)sends data(JSON, XML)then it checks the responses to ensure the efficient working of API.Automatic Scripting: Client URL can be integrated with the shell scripting and it automates some operations like file transfer, performance checks, fetching data etc.
Making your first request using cURL
curl -I https://github.com
HTTP/2 200
server: GitHub.com
content-type: text/html; charset=utf-8
strict-transport-security: max-age=31536000
Request and Response In cURL?
On Internet almost everything works like a conversation between client and server. This convo follows a dedicated rules called HTTP Protocol. So every communication has two parts:
Request - from client to serverResponse - from server to client
To understand client and server lets take an Example: You open Google in browser.
- Client → Your browser/ Curl
Server → Google’s Computer
What exactly a Request is: A request is a structured message sent by the client.
Method (Get, POST)
Get → “Gives me Data”
POST → “Here is data, process it”
Headers ( It Contains about client) It consists:
What browser?
What Language?
What format expected?
Real World Analogy Imagine restaurant:
You(client):
“I want Paneer Butter Masala”
“Table 5”
“Less Spicy”That’s Your Request:
Method → Order food
URL → Kitchen Counter
Header → Preferences
Body → Dish Detail
Response: After server receives a request, it replies with a response.
- Status Code( What happened?)
200 → Success
404 → Not Found
500 → Server Crashed
401 → Unauthorized
- Headers (Extra info about response)
Content Type: text/HTML
Set-cookies
Cache rules
- Body (Actual Data)
HTML page
JSON data
Image
Text
Real Life Analogy Example of that Restaurant Replies:
“Here is your food”
“Order Successfully”
“Bill = 201”It means:
Status → Success
Headers → Billing Detail
Body Actual Food
Using cURL to talk to APIs:
GET and POST are two fundamental HTTP request method used to interact with web server.
GETis used to retrieve information, data, files from a specified source. It is a default method in cURL but you can also use-X GET.
Example: curl https://api.example.com/users/123
curl -X GET https://api.example.com/users/123
POSTis used to send data to a server or to modify a resource.
curl -X POST https://api.example.com/users -H "Content-Type: application/json" -d '{"name":"Jane", "age":23}'
# Using -d implies POST, so -X POST is optional:
curl https://api.example.com/users -H "Content-Type: application/json" -d '{"name":"Jane", "age":23}'
Beginner Mistake with cURL
Thinking cURL is a language but actually cURL is not any programming language, framework, not magic. Its just a tool to send HTTP request from terminal.
Some beginners forget to add protocol schemes (http or https), they just simply write without it EX:
curl example.com.Not understanding properly
GETandPOST.