Print Only HTTP Status Code with Curl

Print Only HTTP Status Code with Curl

When working with HTTP requests, it's often useful to quickly check the status code of a response without displaying the entire body or headers. It can be useful if you're testing the availability of an API endpoint or monitoring website health. This tutorial demonstrates how to print only HTTP status code with a Curl.

Using Curl, the HTTP status code can be printed with the -w (write-out) option. This option lets you specify custom information to be output to standard output after a request is completed. To retrieve the HTTP status code, you can use the http_code variable.

curl -w "%{http_code}" -o NUL -s https://httpbin.org/get
curl -w "%{http_code}" -o /dev/null -s https://httpbin.org/get

The -o option is used to discard the response output, as we are only interested in the HTTP status code. The -s option makes Curl silent, suppressing progress details and additional information.

The command will return HTTP status code such as 200.

Leave a Comment

Cancel reply

Your email address will not be published.