/ curl

Getting HTTP status code by cURL

cURL Command

I often use curl command on the command line to fetch information by HTTP requests. Sometimes I just want to know only the HTTP status code. The option -v, which means verbose printing, prints too much information though I don't need.

% curl -v -sI -o /dev/null kaneshin.co
* Rebuilt URL to: kaneshin.co/
* Hostname was NOT found in DNS cache
*   Trying
* Connected to kaneshin.co ( port 80 (#0)
> HEAD / HTTP/1.1
> User-Agent: curl/7.38.0
> Host: kaneshin.co
> Accept: */*
< HTTP/1.1 200 OK
< Date: Wed, 18 Oct 2017 13:46:01 GMT
< Expires: Wed, 18 Oct 2017 13:56:01 GMT
< ETag: "H2yooA"
< X-Cloud-Trace-Context: c1147593756407630c583009f18acc7d
< Content-Type: text/html
* Server Google Frontend is not blacklisted
< Server: Google Frontend
< Cache-Control: public, max-age=600
< Content-Length: 448
< Age: 170
* Connection #0 to host kaneshin.co left intact

Using -w "%{http_code}"

A more specific way to print out just the HTTP status code is something along the lines of:

% curl -sI -o /dev/null -w "%{http_code}" kaneshin.co

The option -w means to print assigned format below:

-w, --write-out <format>
        Make curl display information on stdout after a completed transfer. The format is a string that may
        contain plain text mixed with any number of variables. The format can be  specified  as  a  literal
        "string",  or  you  can  have curl read the format from a file with "@filename" and to tell curl to
        read the format from stdin you write "@-".

        The variables present in the output format will be substituted by  the  value  or  text  that  curl
        thinks  fit,  as  described  below. All variables are specified as %{variable_name} and to output a
        normal % you just write them as %%. You can output a newline by using \n, a carriage return with \r
        and a tab space with \t.

        NOTE: The %-symbol is a special symbol in the win32-environment, where all occurrences of % must be
        doubled when using this option.

Too simple and many format types are available. show man page to just type man curl.

The option -I might be added to improve response load performance. This parameter just request for status headers of response, without download response body.