If you're learning backend development, you've probably heard "just cURL it." But what is cURL?
Let me explain it simply.
What is a Server?
A server is a computer that waits for requests and sends back responses.
Example:
- You type "google.com"
- Browser asks Google's server for the page
- Server sends back the webpage
- Browser shows it
What is cURL?
cURL is a tool that lets you talk to servers from your terminal.
Simple version:
- Browser talks to servers by clicking and typing URLs
- cURL talks to servers through commands
Why use cURL?
- Test your API quickly
- See raw data servers send back
- Faster than opening a browser
- Good for learning how HTTP works
Your First cURL Command
Open your terminal and type:
curl https://example.com
You'll see HTML code. That's it! You just fetched a webpage.
What happened:
- cURL asked example.com for the page
- Server sent back HTML
- cURL showed it in terminal
Request and Response
Every cURL command has two parts:
Request: What you send to the server
Response: What the server sends back
Your Terminal → Request → Server
Server → Response → Your Terminal
Using cURL with APIs
APIs send back data (usually JSON) instead of webpages.
GET Request (Getting Data)
curl https://jsonplaceholder.typicode.com/users/1
You'll get:
{
"id": 1,
"name": "Leanne Graham",
"email": "leanne@example.com"
}
That's API data!
POST Request (Sending Data)
curl -X POST https://jsonplaceholder.typicode.com/posts \
-H "Content-Type: application/json" \
-d '{"title":"My Post","body":"Hello"}'
What this means:
-
-X POST- We're creating something -
-H- Telling server we're sending JSON -
-d- The actual data
GET vs POST
GET - Asking for data
curl https://api.com/users
POST - Sending data
curl -X POST https://api.com/users -d '{"name":"John"}'
Simple rule:
- Fetching info? Use GET
- Creating/updating? Use POST
Useful Examples
Check if website is working:
curl https://google.com
Save response to a file:
curl https://api.com/data > output.json
See detailed info:
curl -v https://example.com
Common Mistakes
1. Forgetting quotes around JSON:
# Wrong
curl -d {title:test} https://api.com
# Right
curl -d '{"title":"test"}' https://api.com
2. Not using POST for creating data:
# Add -X POST when creating
curl -X POST https://api.com/users -d '{"name":"John"}'
3. Missing Content-Type for JSON:
curl -X POST https://api.com/data \
-H "Content-Type: application/json" \
-d '{"key":"value"}'
When to Use cURL
Testing your API:
curl http://localhost:3000/api/users
Checking if an API works:
curl https://api.github.com/users/octocat
Quick debugging:
curl https://yourapp.com/endpoint
Quick Reference
Basic request:
curl https://example.com
POST with JSON:
curl -X POST https://api.com/data \
-H "Content-Type: application/json" \
-d '{"key":"value"}'
Save to file:
curl https://example.com > file.html
Summary
cURL = Tool to send requests from terminal
Two main types:
- GET - Get data
- POST - Send data
Basic pattern:
curl [URL]
Start simple. Try fetching a page. Then try an API. Build from there.
Top comments (0)