Capturing response metrics with curl using write-out exposes HTTP status codes, timing breakdowns, and transfer sizes for each HTTP request, which simplifies analysis of slow endpoints and validation of API behavior under different conditions. Compact metric summaries make it easier to compare responses over time or across environments without storing full response bodies.
The curl –write-out option expands placeholders such as {http_code}, {time_total}, and {size_download} after the transfer completes, printing a formatted summary to standard output. Combining –write-out with –output for the response body and –silent for progress suppression keeps metrics separate from content while still surfacing errors with –show-error.
On Ubuntu and other Linux distributions, write-out variables behave consistently across HTTP and HTTPS, but certain placeholders remain empty for protocols that do not expose specific timing or size values. Logging formats must account for shell quoting, avoid including sensitive data such as tokens in URLs or headers, and rotate metric logs so repeated requests do not exhaust disk space.
Steps to capture response metrics using cURL write-out on Ubuntu (CLI):
- Define a write-out format file that records key response metrics in a stable, parseable layout.
$ cat > curl-format.txt <<'EOF' http_code=%{http_code} remote_ip=%{remote_ip} remote_port=%{remote_port} scheme=%{scheme} time_namelookup=%{time_namelookup} time_connect=%{time_connect} time_appconnect=%{time_appconnect} time_pretransfer=%{time_pretransfer} time_starttransfer=%{time_starttransfer} time_total=%{time_total} size_download=%{size_download} speed_download=%{speed_download} EOFGrouping write-out variables in a dedicated file keeps metrics consistent across commands and avoids complex shell escaping of {variable} placeholders.
- Send an HTTPS request using curl with the write-out format file and capture the response body separately.
$ curl --silent --show-error --output response.html --write-out @curl-format.txt https://example.org/ http_code=200 remote_ip=93.184.216.34 remote_port=443 scheme=https time_namelookup=0.004321 time_connect=0.015842 time_appconnect=0.078901 time_pretransfer=0.079456 time_starttransfer=0.123456 time_total=0.234567 size_download=1256 speed_download=5357.000000
The –silent option suppresses the progress bar, –show-error keeps error messages visible, and –output response.html stores the body separately from the metrics.
- Append write-out metrics to a log file so every request adds a new set of timing and size values.
$ curl --silent --show-error --output /dev/null --write-out @curl-format.txt https://example.org/ >> curl-metrics.log
Metric logs recorded during frequent or automated requests can grow rapidly and may reveal sensitive paths or query strings, so unbounded logging on shared systems risks information leakage and disk exhaustion.
- Capture metrics for multiple requests in a single run using a simple loop that repeats the curl call.
$ for i in $(seq 1 3); do curl --silent --show-error --output /dev/null --write-out @curl-format.txt https://example.org/ >> curl-metrics.log; echo >> curl-metrics.log; done
Appending an empty line after each run separates metric blocks, which simplifies parsing and visual inspection in tools such as awk or grep.
- Inspect the metrics log and confirm that each request adds a new block of values with the expected status codes and timings.
$ cat curl-metrics.log http_code=200 remote_ip=93.184.216.34 remote_port=443 scheme=https time_namelookup=0.004321 time_connect=0.015842 time_appconnect=0.078901 time_pretransfer=0.079456 time_starttransfer=0.123456 time_total=0.234567 size_download=1256 speed_download=5357.000000 http_code=200 remote_ip=93.184.216.34 remote_port=443 scheme=https time_namelookup=0.003987 time_connect=0.014201 time_appconnect=0.077532 time_pretransfer=0.078044 time_starttransfer=0.120001 time_total=0.230012 size_download=1256 speed_download=5461.000000
Stable values for http_code and reasonable ranges for time_total and speed_download indicate successful requests and usable timing data for trend analysis.
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.
Comment anonymously. Login not required.
