cURL Errors and Reasons - Complete Troubleshooting Guide
Mahesh Waghmare cURL errors can be frustrating when making HTTP requests. Understanding error codes, their causes, and solutions is essential for debugging API calls, web scraping, and HTTP operations.
This comprehensive guide covers all common cURL errors with explanations and solutions.
Introduction to cURL Errors
cURL (Client URL) is a command-line tool and library for transferring data. Errors occur due to network issues, SSL problems, timeouts, or server responses.
Error Format:
- Error codes: Numeric codes (e.g., 6, 7, 28)
- Error messages: Descriptive text
- HTTP status codes: Server response codes
Common Scenarios:
- API requests failing
- SSL certificate issues
- Network connectivity problems
- Timeout errors
- Authentication failures
Common cURL Error Codes
Error Code 6: Couldn’t resolve host
Meaning: DNS resolution failed
Causes:
- Invalid domain name
- DNS server issues
- Network connectivity problems
Solutions:
# Check DNS
nslookup example.com
# Use IP instead
curl http://192.168.1.1
# Specify DNS server
curl --dns-servers 8.8.8.8 http://example.com
Error Code 7: Failed to connect to host
Meaning: Couldn’t connect to server
Causes:
- Server is down
- Firewall blocking
- Wrong port
- Network unreachable
Solutions:
# Check if server is up
ping example.com
# Try different port
curl http://example.com:8080
# Check firewall
# Allow connection through firewall
Error Code 28: Operation timeout
Meaning: Request timed out
Causes:
- Server too slow
- Network latency
- Timeout too short
Solutions:
# Increase timeout
curl --max-time 60 http://example.com
# Or
curl --connect-timeout 30 http://example.com
Connection Errors
Error Code 35: SSL connect error
Meaning: SSL/TLS handshake failed
Solutions:
# Ignore SSL verification (development only)
curl -k https://example.com
# Use specific TLS version
curl --tlsv1.2 https://example.com
# Check certificate
openssl s_client -connect example.com:443
Error Code 51: SSL peer certificate
Meaning: SSL certificate verification failed
Solutions:
# Skip certificate verification (not recommended)
curl -k https://example.com
# Update CA certificates
# Linux: Update ca-certificates package
# macOS: Update via system
# Windows: Update certificate store
Error Code 60: SSL certificate problem
Meaning: Self-signed or invalid certificate
Solutions:
# Accept certificate (development)
curl -k https://example.com
# Add certificate to trust store
# Or fix server certificate
SSL/TLS Errors
Certificate Verification
Disable verification (development only):
curl -k https://example.com
Use specific CA bundle:
curl --cacert /path/to/ca-bundle.crt https://example.com
TLS Version Issues
Specify TLS version:
curl --tlsv1.2 https://example.com
curl --tlsv1.3 https://example.com
Certificate Chain Issues
View certificate:
curl -v https://example.com
Check certificate chain:
openssl s_client -showcerts -connect example.com:443
Timeout Errors
Connection Timeout
Set connection timeout:
curl --connect-timeout 10 http://example.com
Total Timeout
Set maximum time:
curl --max-time 30 http://example.com
Retry on Timeout
curl --retry 3 --retry-delay 5 http://example.com
HTTP Status Errors
400 Bad Request
Meaning: Invalid request
Check:
- Request format
- Headers
- Body content
401 Unauthorized
Meaning: Authentication required
Solution:
curl -u username:password http://example.com
# Or
curl -H "Authorization: Bearer token" http://example.com
403 Forbidden
Meaning: Access denied
Possible causes:
- Insufficient permissions
- IP blocked
- Rate limiting
404 Not Found
Meaning: Resource doesn’t exist
Check:
- URL correctness
- Resource availability
500 Internal Server Error
Meaning: Server error
Action: Contact server administrator
Troubleshooting Guide
Enable Verbose Mode
curl -v http://example.com
Shows detailed connection information.
Check Response Headers
curl -I http://example.com
Save Output for Analysis
curl -v http://example.com -o output.txt 2>&1
Test Connectivity
# Test DNS
nslookup example.com
# Test connectivity
ping example.com
# Test port
telnet example.com 80
Best Practices
1. Always Check Error Codes
curl -f http://example.com || echo "Request failed"
2. Use Appropriate Timeouts
curl --connect-timeout 10 --max-time 30 http://example.com
3. Handle SSL Properly
- Verify certificates in production
- Use proper CA bundles
- Don’t disable verification in production
4. Log Errors
curl http://example.com 2>&1 | tee curl.log
5. Retry Logic
curl --retry 3 --retry-delay 5 http://example.com
Common Error Reference
- 6: Couldn’t resolve host
- 7: Failed to connect
- 28: Timeout
- 35: SSL connect error
- 51: SSL peer certificate
- 60: SSL certificate problem
Conclusion
cURL errors indicate:
- Network issues - Connection problems
- SSL problems - Certificate issues
- Server errors - HTTP status codes
- Configuration - Timeout, retry settings
Key Points:
- Use verbose mode (
-v) for debugging - Check error codes for specific issues
- Handle SSL properly
- Set appropriate timeouts
- Implement retry logic
Understanding cURL errors helps debug HTTP requests and API integrations effectively.
Written by Mahesh Waghmare
I bridge the gap between WordPress architecture and modern React frontends. Currently building tools for the AI era.
Follow on Twitter →