How to display HTTP response code

Use curl utility to verify HTTP response code for given URL.

The simplest way to display HTTP response code is to use quiet mode, discard output and display the numerical response code that was found in the last retrieved transfer after executing GET method.

$ curl --silent --output /dev/null --write-out "%{http_code}\n" http://sleeplessbeastie.eu
301

The above-mentioned solution does not follow redirects, so we need to follow Location: header and a 3XX response code.

$ curl --location --silent --output /dev/null --write-out "%{http_code}\n" http://sleeplessbeastie.eu
200

This solution can be upgraded further to fetch only headers instead of the whole document, but you need to be aware that HEAD method can be blocked on the server side, which is kind of silly as the HEAD method is identical to the default GET except that it does not return message body - only headers.

$ curl --head --location --silent --output /dev/null --write-out "%{http_code}\n" http://sleeplessbeastie.eu
200
Milosz Galazka's Picture

About Milosz Galazka

Milosz is a Linux Foundation Certified Engineer working for a successful Polish company as a system administrator and a long time supporter of Free Software Foundation and Debian operating system.

Gdansk, Poland https://sleeplessbeastie.eu