Write out

--write-out or just -w for short, outputs text and information after a transfer is completed. It offers a large range of variables that you can include in the output, variables that have been set with values and information from the transfer.

Instruct curl to output a string by passing plain text to this option:

curl -w "formatted string" http://example.com/

…and you can also have curl read that string from a given file instead if you prefix the string with '@':

curl -w @filename http://example.com/

…or even have curl read the string from stdin if you use '-' as filename:

curl -w @- http://example.com/

Variables

The variables that are available are accessed by writing %{variable_name} in the string and that variable is substituted by the correct value. To output a plain % you write it as %%. You can also output a newline by using \n, a carriage return with \r and a tab space with \t.

As an example, we can output the Content-Type and the response code from an HTTP transfer, separated with newlines and some extra text like this:

curl -w "Type: %{content_type}\nCode: %{response_code}\n" \
  http://example.com

The output is sent to stdout by default so you probably want to make sure that you do not also send the downloaded content to stdout as then you might have a hard time to separate out the data; or use %{stderr} to send the output to stderr.

HTTP headers

This option also provides an easy to use way to output the contents of HTTP response headers from the most recent transfer.

Use %header{name} in the string, where name is the case insensitive name of the header (without the trailing colon). The output header contents are then shown exactly as was sent over the network, with leading and trailing whitespace trimmed. Like this:

curl -w "Server: %header{server}\n" http://example.com

Output

By default, this option makes the selected data get output on stdout. If that is not good enough, the pseudo-variable %{stderr} can be used to direct (the following) part to stderr and %{stdout} brings it back to stdout.

From curl 8.3.0, there is a feature that lets users send the write-out output to a file: %output{filename}. The data following is then written to that file. If you would rather have curl append to that file instead of creating it from scratch, prefix the filename with >>. Like this: %output{>>filename}.

A write-out argument can include output to stderr, stdout and files as the user sees fit.

Windows

NOTE: In Windows, the %-symbol is a special symbol used to expand environment variables. In batch files all occurrences of % must be doubled when using this option to properly escape. If this option is used at the command prompt then the % cannot be escaped and unintended expansion is possible.

Available --write-out variables

Some of these variables are not available in really old curl versions.

VariableDescription
certsOutputs the certificate chain from the most recent TLS handshake - with details. (Introduced in 7.88.0)
conn_idThe connection identifier last used by the transfer. The connection id is unique number among all connections using the same connection cache. (Introduced in 8.2.0)
content_typeContent-Type of the requested document, if there was any.
errormsgError message from the transfer. Empty if no error occurred. (Introduced in 7.75.0)
exitcodeNumerical exit code from the transfer. 0 if no error occurred. (Introduced in 7.75.0)
filename_effectiveThe ultimate filename that curl writes out to. Practical if curl is told to write to a file with the --remote-name or --output option. It is most useful in combination with the --remote-header-name option.
ftp_entry_pathThe initial path curl ended up in when logging on to the remote FTP server.
header_jsonA JSON object with all HTTP response headers from the recent transfer. Values are provided as arrays, since in the case of multiple headers there can be multiple values. (Introduced in 7.83.0)
http_codeThe former variable name for what is now known as response_code.
http_connectthe numerical code that was found in the last response (from a proxy) to a curl CONNECT request.
http_versionThe HTTP version that was used.
jsonall write-out variables as a single JSON object. (Introduced in 7.72.0)
local_ipIP address of the local end of the most recently used connection - can be either IPv4 or IPv6
local_portLocal port number of the most recently used connection
methodHTTP method the most recent request used. (Introduced in 7.72.0)
num_certsNumber of the certificates in the most recent TLS handshake. (Introduced in 7.88.0)
num_connectsNumber of new connects made in the recent transfer.
num_headersNumber of response headers in the last response
num_redirectsNumber of redirects that were followed in the request.
num_retriesNumber of retries actually performed when --retry has been used. (Introduced in 8.9.0)
onerrorIf the transfer ended with an error, show the rest of the string, otherwise stop here. (Introduced in 7.75.0)
proxy_ssl_verify_resultThe result of the SSL peer certificate verification that was requested when communicating with a proxy. 0 means the verification was successful.
proxy_usedReturns 1 if the previous transfer used a proxy, otherwise 0. Useful to for example determine if a NOPROXY pattern matched the hostname or not. (Introduced in 8.7.0)
redirect_urlThe actual URL a redirect would take you to when an HTTP request was made without -L to follow redirects.
refererThe Referer: header, if there was any. (Introduced in 7.76.0)
remote_ipThe remote IP address of the most recently used connection — can be either IPv4 or IPv6.
remote_portThe remote port number of the most recently made connection.
response_codeThe numerical response code that was found in the last transfer.
schemescheme used in the previous URL
size_downloadTotal number of bytes that were downloaded.
size_headerTotal number of bytes of the downloaded headers.
size_requestTotal number of bytes that were sent in the HTTP request.
size_uploadTotal number of bytes that were uploaded.
speed_downloadAverage download speed that curl measured for the complete download in bytes per second.
speed_uploadAverage upload speed that curl measured for the complete upload in bytes per second.
ssl_verify_resultthe result of the SSL peer certificate verification that was requested. 0 means the verification was successful.
stderrMakes the rest of the output get written to stderr.
stdoutmakes the rest of the output get written to stdout.
time_appconnectThe time in seconds, it took from the start until the SSL/SSH/etc connect/handshake to the remote host was completed.
time_connectThe time in seconds, it took from the start until the TCP connect to the remote host (or proxy) was completed.
time_namelookupThe time in seconds, it took from the start until the name resolving was completed.
time_pretransferThe time in seconds, it took from the start until the file transfer was just about to begin. This includes all pre-transfer commands and negotiations that are specific to the particular protocol(s) involved.
time_redirectThe time in seconds, it took for all redirection steps including name lookup, connect, pre-transfer and transfer before the final transaction was started. time_redirect the complete execution time for multiple redirections.
time_starttransferThe time in seconds, it took from the start until the first byte was just about to be transferred. This includes time_pretransfer and also the time the server needed to calculate the result.
time_totalThe total time in seconds, that the full operation lasted. The time is displayed with millisecond resolution.
urlThe URL used in the transfer. (Introduced in 7.75.0)
url_effectiveThe URL that was fetched last. This is particularly meaningful if you have told curl to follow Location: headers (with -L).
urlnum0-based numerical index of the URL used in the transfer. (Introduced in 7.75.0)
xfer_idThe numerical identifier of the last transfer done. -1 if no transfer has been started yet for the handle. The transfer id is unique among all transfers performed using the same connection cache. (Introduced in 8.2.0)

In curl 8.1.0, variables to output only specific URL components were added, for when the url or url_effective variables show more than you want.

VariableDescription
url.fragmentThe fragment part of the URL that was fetched.
url.hostThe hostname part of the URL that was fetched.
url.optionsThe options part of the URL that was fetched. Only available for some schemes.
url.passwordThe password part of the URL that was fetched.
url.pathThe path part of the URL that was fetched.
url.portThe port number of the URL that was fetched.
url.queryThe query part of the URL that was fetched.
url.schemeThe scheme part of the URL that was fetched.
url.userThe user part of the URL that was fetched.
url.zoneidThe zone id part of the URL that was fetched. Only available if the hostname is an IPv6 address.
urle.fragmentThe fragment part of the effective (last) URL that was fetched.
urle.hostThe hostname part of the effective (last) URL that was fetched.
urle.optionsThe options part of the effective (last) URL that was fetched. Only available for some schemes.
urle.passwordThe password part of the effective (last) URL that was fetched.
urle.pathThe path part of the effective (last) URL that was fetched.
urle.portThe port number of the effective (last) URL that was fetched.
urle.queryThe query part of the effective (last) URL that was fetched.
urle.schemeThe scheme part of the effective (last) URL that was fetched.
urle.userThe user part of the effective (last) URL that was fetched.
urle.zoneidThe zone id part of the effective (last) URL that was fetched. Only available if the hostname is an IPv6 address.