10 min to read
Understanding and Using Curl - A Comprehensive Guide
A detailed guide to using Curl for HTTP requests and API testing

Introduction to Curl
Before diving deep into Curl, it's recommended to understand HTTP methods, which you can find in my previous post: HTTP Methods and Status Codes.
Curl offers several key capabilities that make it indispensable for development and troubleshooting:
- Universal Protocol Support: Works with numerous protocols beyond just HTTP
- Cross-Platform Compatibility: Available on virtually all operating systems
- Scriptability: Easily incorporated into shell scripts and automation
- No GUI Dependencies: Works in headless environments and terminals
- Extensive Documentation: Comprehensive man pages and online resources
- Active Development: Regular updates and security patches
Curl Command Structure
Basic Syntax
The basic syntax for curl commands follows this pattern:
curl [options] [URL]
For example, a simple GET request would look like:
curl https://www.example.com
While a more complex POST request might be:
curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' https://api.example.com/endpoint
Common Curl Options
Category | Option | Description |
---|---|---|
Request Methods | -X, --request | Specifies the request method to use (GET, POST, PUT, DELETE, etc.) |
-G, --get | Forces the request to be sent as GET even with data parameters | |
Headers and Data | -H, --header | Adds a custom header to the request (can be used multiple times) |
-d, --data | Sends data in the request body, typically with POST, PUT (automatically sets method to POST) | |
--json | Sets Content-Type header to application/json and data format to JSON | |
Output Control | -o, --output | Writes output to a specified file instead of stdout |
-O, --remote-name | Saves the output using the name of the remote file | |
-s, --silent | Suppresses progress meters and error messages | |
-v, --verbose | Makes curl display detailed information about the request and response | |
Connection Options | -L, --location | Follows HTTP redirects, useful for websites that redirect requests |
-k, --insecure | Allows insecure connections when using SSL, skipping certificate validation | |
--connect-timeout | Sets maximum time allowed for connection in seconds | |
Authentication | -u, --user | Provides username and password for server authentication |
--oauth2-bearer | Sends OAuth 2.0 Bearer Token in the Authorization header |
Basic Curl Usage Examples
HTTP Request Methods
Fetch the content of a webpage:
curl https://www.example.com
Send form data to a server:
curl -X POST -d "param1=value1¶m2=value2" https://www.example.com/form
Submit JSON data to an API endpoint:
Update an existing resource:
Remove a resource from the server:
curl -X DELETE https://api.example.com/resources/123
Handling Output
Save the response body to a specific file:
# Save with custom name
curl -o example.html https://www.example.com
# Save with original filename
curl -O https://www.example.com/file.zip
Check HTTP headers without downloading the body:
curl -I https://www.example.com
Show detailed information about the request and response:
curl -v https://www.example.com
Content-Type: application/x-www-form-urlencoded
param1=value1¶m2=value2 Server-->>Client: 200 OK (Response) Note over Client,Server: JSON API Request Client->>Server: POST /api HTTP/1.1
Content-Type: application/json
{"key":"value"} Server-->>Client: 201 Created (JSON Response)
Common Connection Options
Automatically follow HTTP redirect responses:
curl -L https://github.com
Limit the connection time to prevent hanging:
curl --connect-timeout 5 https://slow-server.example.com
Bypass SSL verification for testing or self-signed certificates (use with caution):
curl -k https://self-signed.badssl.com/
Advanced Curl Techniques
Working with APIs
Create a pull request using GitHub's API:
Search for repositories on GitHub:
Authentication Methods
Access resources that require username and password:
curl -u username:password https://api.example.com/protected-resource
Use OAuth 2.0 or JWT tokens for authentication:
Cookies and Sessions
Maintain session state across requests:
File Upload
Send files to a server using multipart/form-data:
Set content type and filename for uploaded files:
Practical Curl Use Cases
Network Diagnostics
Quickly determine your public IP address:
curl -s ipinfo.io/ip
# or
curl ifconfig.me
Verify DNS resolution by forcing curl to use a specific IP:
curl -v --resolve example.com:443:93.184.216.34 https://example.com
Web Services and Information
Get current weather conditions in a terminal-friendly format:
curl wttr.in/Seoul
Get the latest exchange rates:
curl -s https://api.exchangerate-api.com/v4/latest/USD | jq
HTTP Server Testing
Perform basic load testing with multiple requests:
# Make 10 concurrent requests
for i in {1..10}; do
curl -s https://example.com > /dev/null &
done
Measure how long it takes to connect to a website and receive data:
Best Practices and Tips
Security Considerations
- Avoid Exposing Credentials: Don't include credentials directly in command lines as they may be visible in process lists or command history
- Use Environment Variables: Store tokens and passwords in environment variables instead of directly in scripts
- Clear History: Clear command history after running commands with sensitive information
- Prefer .netrc for Stored Credentials: Use the .netrc file with proper permissions for recurring authentication
- Be Cautious with -k/--insecure: Only bypass SSL verification in controlled test environments, never in production
Scripting Efficiency
- Use Silent Mode in Scripts: Always use -s (silent) in scripts to suppress progress meters
- Add Error Handling: Check exit codes to handle failures gracefully
- Set Timeouts: Always set appropriate timeouts to prevent scripts from hanging indefinitely
- Parse Responses Properly: Use tools like jq for JSON or grep/sed for text parsing
- Follow Redirects When Needed: Include -L for sites that might redirect
Here's an example of a curl script following best practices:
Key Points
-
Core Capabilities
- Supports over 25 protocols including HTTP, HTTPS, FTP, FTPS, SFTP
- Works on virtually all operating systems and environments
- Handles all common HTTP methods (GET, POST, PUT, DELETE, etc.)
- Offers comprehensive header and request customization -
Common Use Cases
- API testing and interaction
- Downloading and uploading files
- Debugging network and web services
- Automation and scripting of web requests
- Web service health checks and monitoring -
Best Practices
- Use environment variables for sensitive data
- Include proper error handling in scripts
- Set appropriate timeouts for requests
- Follow redirects when working with web apps
- Use silent mode in scripts and with proper parsing
Comments