Advanced Connectivity Testing with cURL
Now that you understand basic connectivity testing, let us explore more advanced features of cURL that can help with detailed troubleshooting and testing.
Using Verbose Mode for Detailed Debugging
The verbose mode (-v
option) is invaluable for troubleshooting connectivity issues as it shows the entire request and response process:
curl -v https://example.com
The output will be comprehensive, showing the DNS resolution, TLS handshake, request headers, response headers, and more:
* Trying 93.184.216.34:443...
* Connected to example.com (93.184.216.34) port 443 (#0)
* ALPN: offers h2
* ALPN: offers http/1.1
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
* TLSv1.3 (IN), TLS handshake, Certificate (11):
* TLSv1.3 (IN), TLS handshake, CERT verify (15):
* TLSv1.3 (IN), TLS handshake, Finished (20):
* TLSv1.3 (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* ALPN: server accepted h2
* Server certificate:
* subject: C=US; ST=California; L=Los Angeles; O=Internet Corporation for Assigned Names and Numbers; CN=www.example.org
* start date: Nov 24 00:00:00 2022 GMT
* expire date: Nov 24 23:59:59 2023 GMT
* subjectAltName: host "example.com" matched cert's "example.com"
* issuer: C=US; O=DigiCert Inc; CN=DigiCert TLS RSA SHA256 2020 CA1
* SSL certificate verify ok.
* using HTTP/2
* h2 [:method: GET]
* h2 [:path: /]
* h2 [:scheme: https]
* h2 [:authority: example.com]
* h2 [user-agent: curl/7.81.0]
* h2 [accept: */*]
* Using Stream ID: 1
> GET / HTTP/2
> Host: example.com
> user-agent: curl/7.81.0
> accept: */*
>
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* Connection state changed (MAX_CONCURRENT_STREAMS == 100)!
< HTTP/2 200
< age: 587269
< cache-control: max-age=604800
< content-type: text/html; charset=UTF-8
< date: Tue, 28 Mar 2023 12:40:01 GMT
< etag: "3147526947+ident"
< expires: Tue, 04 Apr 2023 12:40:01 GMT
< last-modified: Thu, 17 Oct 2019 07:18:26 GMT
< server: ECS (nyb/1D2B)
< vary: Accept-Encoding
< x-cache: HIT
< content-length: 1256
<
<!doctype html>
<html>
<head>
<title>Example Domain</title>
<!-- More HTML content -->
</head>
<body>
<div>
<h1>Example Domain</h1>
<p>This domain is for use in illustrative examples in documents...</p>
<!-- More content -->
</div>
</body>
</html>
* Connection #0 to host example.com left intact
This detailed output helps you identify where exactly a connection might be failing.
Testing Different HTTP Methods
Let us test a POST request to a test API endpoint:
curl -X POST -d "name=test&[email protected]" https://httpbin.org/post
This command:
-X POST
: Specifies a POST request
-d "name=test&[email protected]"
: Sends form data in the request
You should receive a JSON response showing your submitted data:
{
"args": {},
"data": "",
"files": {},
"form": {
"email": "[email protected]",
"name": "test"
},
"headers": {
"Accept": "*/*",
"Content-Length": "32",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "curl/7.81.0",
"X-Amzn-Trace-Id": "Root=1-642295b1-0d2340ef34f2e8ea6270241a"
},
"json": null,
"origin": "198.51.100.42",
"url": "https://httpbin.org/post"
}
Many APIs require specific headers for authentication or to specify the content type. Let us test this:
curl -H "User-Agent: MyCustomAgent" -H "Authorization: Bearer test-token" https://httpbin.org/headers
This command:
-H "User-Agent: MyCustomAgent"
: Sets a custom User-Agent header
-H "Authorization: Bearer test-token"
: Sets an Authorization header
The response will show the headers sent in your request:
{
"headers": {
"Accept": "*/*",
"Authorization": "Bearer test-token",
"Host": "httpbin.org",
"User-Agent": "MyCustomAgent",
"X-Amzn-Trace-Id": "Root=1-642295c3-73cac0a73b34b1c93a8ce520"
}
}
Testing Response Times for Different Endpoints
Let us create a script to compare response times for different servers:
nano response_time.sh
Add the following content:
#!/bin/bash
echo "Testing response times..."
for site in google.com bing.com baidu.com duckduckgo.com yahoo.com; do
echo -n "$site: "
curl -s -o /dev/null -w "%{time_total}s" "https://$site"
echo ""
done
echo "Testing complete!"
Save the file and make it executable:
chmod +x response_time.sh
Run the script:
./response_time.sh
The output will show the response time for each site:
Testing response times...
google.com: 0.187s
bing.com: 0.232s
baidu.com: 0.412s
duckduckgo.com: 0.298s
yahoo.com: 0.342s
Testing complete!
This is useful for comparing the performance of different servers or monitoring a server's performance over time.
Testing TCP Connectivity to Specific Ports
Sometimes you need to test if a specific port is open on a server. cURL can be used for this as well:
curl -v telnet://example.com:80
If the port is open, you will see a successful connection message:
* Trying 93.184.216.34:80...
* Connected to example.com (93.184.216.34) port 80 (#0)
Press Ctrl+C
to terminate the connection.
Similarly, you can test secure connections:
curl -v https://example.com:443
The verbose output will show if the connection was successful or if there were any issues.