Tuesday, October 11, 2016

Getting HTTP status code by cURL

I often use cURL command at the command line on Linux and OS X 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.

-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}" http://www.example.org/
The option -w means to print assigned format below:
-w, --write-out 
        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.

No comments:

Post a Comment