Getting started with cURL

What is cURL?
cURL (short for “Client URL”) is a command-line tool that lets you send requests to servers from your terminal and see their responses. It’s like using your terminal to “talk” to websites or APIs instead of a browser.
Why programmers need cURL
Quick connectivity checks: “Is this server/API reachable?”
Inspecting responses: See raw HTML, JSON, or error messages to debug.
Fast iteration: Test endpoints without writing code or opening a browser.
Making your first request using cURL
Reminder: A server is a computer on the internet that listens for requests and sends back responses (web pages, API data, files).
Simplest GET request (fetch a web page):
curl https://example.com
This sends a GET request and prints the response body (e.g., the HTML) in your terminal.
Tip: Start with just curl URL to build confidence. Add options later.
Understanding request and response
Request: Method (GET or POST), URL, optional headers/body.
Response parts:
Status code (200 OK, 404 Not Found, 500 Server Error, etc.).
Headers (metadata like Content-Type: application/json).
Body (the actual content: HTML, JSON, text, etc.).
By default, curl shows you the body. To see headers too, add -i (later, once you’re comfortable).
Using cURL to talk to APIs
GET (fetch data):
Typically returns JSON.
POST (send data):
curl -X POST -d "name=Alex&role=admin" https://api.example.com/users
-X POST sets the method; -d adds form data to the request body.
For JSON APIs, a minimal example (when ready to add a header):
- curl -X POST -H "Content-Type: application/json" -d '{"name":"Alex","role":"admin"}' https://api.example.com/users
Start with simple GET; add POST and minimal headers when needed.
Common mistakes beginners make with cURL
Using the wrong URL (missing https:// or the wrong endpoint).
Forgetting data with POST (no -d) or forgetting to set the method (-X POST).
Expecting JSON but hitting an HTML page (wrong endpoint or URL).
Adding too many flags too soon; stick to curl URL first, then layer options gradually.






