@Ajit5ingh

cURL Commands

Essential HTTP client commands every developer should know

What is cURL?

cURL is a command-line tool for making HTTP requests. Think of it as your browser's developer tools, but for the terminal. It lets you send requests, download files, and test APIs without opening a browser.

Basic Requests

Simple GET Request
The most basic way to fetch data from an API
curl https://api.github.com/users/octocat
{ "login": "octocat", "id": 1, "name": "The Octocat" }
POST Request with JSON
Send data to create or update resources
curl -X POST \ -H "Content-Type: application/json" \ -d '{"name":"John","email":"john@example.com"}' \ https://api.example.com/users
Download a File
Save content directly to your local machine
curl -o filename.zip https://example.com/file.zip # Or use the remote filename curl -O https://example.com/file.zip

Authentication Examples

Basic Auth
Username and password authentication
curl -u username:password https://api.example.com/data # Or encode it yourself curl -H "Authorization: Basic dXNlcjpwYXNz" https://api.example.com/data
Bearer Token
API token authentication (most common for modern APIs)
curl -H "Authorization: Bearer your-token-here" \ https://api.example.com/protected
API Key in Header
Custom header for API key authentication
curl -H "X-API-Key: your-api-key" \ https://api.example.com/data

Advanced Usage

Follow Redirects
Automatically follow HTTP redirects
curl -L https://bit.ly/short-url
Show Response Headers
Include headers in the output for debugging
curl -i https://api.example.com/data # Or just headers curl -I https://api.example.com/data
Upload a File
Send files via form data or multipart
curl -X POST \ -F "file=@/path/to/file.jpg" \ -F "description=My photo" \ https://api.example.com/upload
Set Timeout
Prevent hanging on slow connections
curl --connect-timeout 10 \ --max-time 30 \ https://api.example.com/data
Ignore SSL Errors
Skip certificate validation (use carefully!)
curl -k https://self-signed.example.com # Or be more specific curl --insecure https://self-signed.example.com
Save Cookies
Handle session cookies for authenticated flows
# Save cookies curl -c cookies.txt https://example.com/login # Use saved cookies curl -b cookies.txt https://example.com/dashboard

Testing Different HTTP Methods

PUT Request
Update an existing resource
curl -X PUT \ -H "Content-Type: application/json" \ -d '{"name":"Updated Name"}' \ https://api.example.com/users/123
DELETE Request
Remove a resource
curl -X DELETE \ -H "Authorization: Bearer token" \ https://api.example.com/users/123
PATCH Request
Partial updates to a resource
curl -X PATCH \ -H "Content-Type: application/json" \ -d '{"email":"new@example.com"}' \ https://api.example.com/users/123
← Back to All Explainers