curl -w Option Guide
In everyday web development and operations work, the curl
command is an incredibly powerful tool. It can simulate requests from various browsers, perform debugging and testing. Not only can curl
initiate HTTP requests, but it can also provide very detailed response information, such as HTTP status codes, request time, and more. By using the -w
option, curl
can output detailed statistics about the request, helping developers monitor and optimize web application performance.
This article will detail how to use the curl -w
option to get HTTP request status codes, request time, response content type, and other performance metrics.
Get HTTP Status Code
To get the HTTP status code of a request, curl
provides a very simple method. Use the -w
option with the %{http_code}
variable to output the HTTP status code of the response.
Example:
1 |
|
-s
: Silent mode,curl
will not output the progress bar or other extra information.-w '%{http_code}'
: Specifies the output for the HTTP status code.-o /dev/null
: Discards the response body, only the status code is shown.
Output:
1 |
|
A status code of 200
means the request was successful.
Get Total Request Time
If you need to get the total time of an HTTP request, you can use the %{time_total}
variable. This value represents the total time from making the request to receiving the response, in seconds.
Example:
1 |
|
Output:
1 |
|
This indicates that the total time from making the request to receiving the response was 0.4803 seconds.
Get Detailed Time Consumption
In addition to total time, curl
provides multiple time-related metrics to help us more precisely analyze each phase of the request’s performance. Common time consumption metrics include:
time_namelookup
: Time spent on domain name lookup.time_connect
: Time spent establishing the TCP connection (i.e., the time for the three-way handshake).time_redirect
: Time spent handling redirects.time_pretransfer
: Time from the start of the request until the response starts transferring.time_starttransfer
: Time from the start of the request until the first byte is ready to transfer.
Example:
1 |
|
Output:
1 |
|
With these metrics, you can more precisely analyze the performance of the request and identify bottlenecks, whether they are related to DNS lookup, connection time, redirection, or transfer time.
Get Response Content Type
If you need to check the content type of the response (e.g., whether the returned data is an HTML page, JSON, or something else), you can use %{content_type}
to get the Content-Type
field from the response headers.
Example:
1 |
|
Output:
1 |
|
This indicates that the returned content is in HTML format and the character encoding is UTF-8.
Output Multiple Metrics
The curl -w
option allows you to output multiple performance metrics at once. You can include multiple variables in the format string and separate them with spaces or line breaks.
1 |
|
Output:
1 |
|
This command will output both the total request time (in seconds) and the HTTP status code.
Output on Different Lines
If you prefer each metric to be output on different lines, you can use \n
to break the lines.
1 |
|
Output:
1 |
|
Here, we separated the total time and status code onto two lines.
Summary
By using the curl -w
option, developers can easily obtain various performance metrics of HTTP requests, including status codes, total request time, time consumption for each stage, and response content type. Mastering these techniques can help you better debug and optimize web applications, improving website response speed and stability.
Commonly Used curl -w
Options
Field | Description |
---|---|
%{http_code} |
The HTTP status code, which indicates the result of an HTTP request. Different status codes have different meanings; for example, 200 means the request was successful, while 404 means the resource was not found. |
%{time_total} |
Total time (in seconds) taken from the moment the HTTP request is made until the full response is received. This value provides a comprehensive reflection of the overall request process duration. |
%{time_namelookup} |
DNS lookup time (in seconds), the time taken to resolve the domain name to an IP address. If the DNS resolution is slow, it will impact subsequent steps like connection and data transfer. |
%{time_connect} |
TCP connection time (in seconds), the time it takes to establish a TCP connection, including the time for the three-way handshake. |
%{time_redirect} |
Redirect time (if applicable), the time spent handling redirects when the request encounters one or more 3xx HTTP status codes. |
%{time_pretransfer} |
Time (in seconds) from the start of the request until the response begins to transfer. This includes DNS resolution, TCP connection, and initial transfer preparation. |
%{time_starttransfer} |
Time (in seconds) from the start of the request until the first byte of the response starts to transfer. This metric shows when the server begins to respond. |
%{content_type} |
The Content-Type of the response (e.g., text/html ), indicating the format of the returned data. |
This translation preserves the meaning of the original content while adapting it to American English. The technical explanations, command examples, and descriptions remain clear and informative for the readers.
curl -w Option Guide