How to output HTTP status code using cURL
Summary
This article explains two reliable command-line methods to make curl output only the numerical HTTP status code. Each curl option and shell utility is explained in detail, including dependencies and references.
Introduction #
When you use curl, a free and open source command-line tool for transferring data using Uniform Resource Locators (URLs), you often need to know whether a request succeeded.
One common requirement is to retrieve only the Hypertext Transfer Protocol (HTTP) status code as a numeric value, without the full HTTP headers or response bodies.
This article explains two command-line approaches to make curl output only the numerical HTTP status code. Each option and supporting command is explained in detail so that you understand exactly how the bare HTTP status code output is produced.
Option 1 relies entirely on `curl` built-in features. It is more robust, easier to read, and preferred for scripts and automation.
Option 1 #
This approach uses curl built-in functionality to print the HTTP status code directly, without relying on external text processing tools.
curl -s -o /dev/null -w "%{http_code}" http://www.example.org/
Option 1 - Command breakdown #
-s
The-soption enables silent mode.
Silent mode disables the progress meter and suppresses error messages. This ensures that only the desired output is written to standard output.-o /dev/null
The-ooption specifies an output file for the response body.
By setting the output file to/dev/null, the response body is discarded. This prevents any response content from appearing in the output.-w "%{http_code}"
The-woption tellscurlto output specific variables after a request completes.
The format string%{http_code}instructscurlto print only the HTTP response status code as a numeric value. No additional text or line breaks are added unless explicitly specified.
This method produces output such as:
200
That is exactly the bare result of a HTTP status code, we are looking for.
Option 2 #
This approach depends on additional command-line utilities such as head and cut. It parses the HTTP status line manually and is sensitive to changes in output format.
This approach relies on requesting only the HTTP response headers using curl -I and extracting the status code from the first line of the response using shell utilities.
curl -I http://www.example.org 2>/dev/null | head -n 1 | cut -d$' ' -f2
Option 2 - Command breakdown #
curl -I
The-Ioption tellscurlto fetch the HTTP headers only.
Internally, this makescurlsend an HTTP HEAD request instead of an HTTP GET request. A HEAD request retrieves the response headers without downloading the response body. The first line of the response headers is the HTTP status line, which contains the protocol version, status code, and status text.
Example of a typical status line:HTTP/1.1 200 OK2>/dev/null
The2>/dev/nullpart is shell redirection.
Standard error, abbreviated as stderr, is file descriptor 2.curlwrites progress information and certain warnings to stderr. Redirecting stderr to/dev/nulldiscards this output so that it does not interfere with further processing in the pipeline.- pipe operator
|
The pipe operator passes the standard output of one command as the input to the next command.
In this case, the HTTP headers produced bycurlare passed sequentially toheadand then tocut. head -n 1
Theheadcommand outputs the first lines of its input.
The-n 1option tellsheadto output only the first line. This line contains the HTTP status line, which includes the numerical status code.cut -d$' '
Thecutcommand extracts specific fields from a line of text.
The-d$' 'option sets the field delimiter to a single space character. The$' 'syntax ensures that the delimiter is interpreted correctly as a space.-f2
The-f2option selects the second field.
In an HTTP status line, the second field is the numerical HTTP status code. For example, inHTTP/1.1 200 OK, the value200is returned.
This pipeline produces output such as:
200
That is also exactly the bare result of a HTTP status code, we are looking for.
FAQ's #
Most common questions and brief, easy-to-understand answers on the topic:
Does curl always return an HTTP status code?
No. curl returns an HTTP status code only when it successfully receives an HTTP response. Network failures or protocol errors prevent a status code from being returned.
Is -I the same as a HEAD request?
Yes. The -I option tells curl to perform an HTTP HEAD request, which retrieves response headers without the message body.
Why is /dev/null used in these examples?
/dev/null discards output. It is used to suppress response bodies or headers that are not needed.
Can these curl commands be used in shell scripts?
Yes. Both approaches are suitable for shell scripts and are commonly used for monitoring, automation, and diagnostics.
Further readings #
Sources and recommended, further resources on the topic:
- Curl: Command line tool and library for transferring data with URLs
- cURL manual: Man page
- cURL manual:
-I/--headfetch the headers only option. - cURL manual:
-s/--silentoption - cURL manual:
-o/--outputoption - cURL manual:
-w/--write-outoption - cURL cheat sheet
- List of HTTP status codes
License
How to output HTTP status code using cURL by Jonas Jared Jacek is licensed under CC BY-SA 4.0.
This license requires that reusers give credit to the creator. It allows reusers to distribute, remix, adapt, and build upon the material in any medium or format, for noncommercial purposes only. To give credit, provide a link back to the original source, the author, and the license e.g. like this:
<p xmlns:cc="http://creativecommons.org/ns#" xmlns:dct="http://purl.org/dc/terms/"><a property="dct:title" rel="cc:attributionURL" href="https://www.ditig.com/how-to-output-http-status-code-with-curl">How to output HTTP status code using cURL</a> by <a rel="cc:attributionURL dct:creator" property="cc:attributionName" href="https://www.j15k.com/">Jonas Jared Jacek</a> is licensed under <a href="https://creativecommons.org/licenses/by-sa/4.0/" target="_blank" rel="license noopener noreferrer">CC BY-SA 4.0</a>.</p>For more information see the Ditig legal page.