cURL : Client URL
cURL is an open-source command-line tool used for transferring data between a client and a server.
- Supports HTTP, HTTPS, FTP, SCP, SFTP, and many more protocols
Basic cURL Commands
1. Fetch a Web Page
curl https://example.com
2. Download a File
curl -O https://example.com/file.zip
3. Send a GET Request
curl https://api.example.com/data
4. Send a POST Request
curl -X POST https://api.example.com/users \
-H "Content-Type: application/json" \
-d '{"name": "Lokesh", "email": "lokesh@example.com"}'
5. Send a Request with Authentication
For API authentication, use:
curl -u username:password https://api.example.com/auth
Or use a token:
curl -H "Authorization: Bearer YOUR_ACCESS_TOKEN" https://api.example.com/data
6. Upload a File: Upload a file using the -F
flag:
curl -X POST -F "file=@document.pdf" https://example.com/upload
7. Save Output to a File
Redirect the output to a file:
curl https://example.com -o output.html
Advanced cURL Usage
1. Handling Redirects
Follow redirects with:
curl -L https://shorturl.com
2. Setting Timeout
Set a request timeout:
curl --max-time 10 https://example.com
3. Debugging Requests
See detailed request and response logs:
curl -v https://example.com
4. Using cURL in Scripts
Automate API calls in scripts:
#!/bin/bash
response=$(curl -s https://api.example.com/data)
echo "API Response: $response"
cURL is an essential tool for developers, system administrators, and DevOps engineers. It simplifies API testing, file transfers, and automation tasks.