How to use curl to test for server response codes?

Published on Aug. 22, 2023, 12:19 p.m.

To use curl to test for server response codes, you can use the -I flag to send a HEAD request and receive only the headers, and then use the -w flag with the %{http_code} format specifier to print the status code. Here’s an example:

curl -I -w '%{http_code}' http://www.example.com

In this example, we’re sending a HEAD request to http://www.example.com by using the -I flag. The -w flag is used to specify a custom output format, and %{http_code} is a format specifier that will print the HTTP status code returned by the server.

When you run this command, it will output the HTTP status code, which you can use to determine if the server is responding properly. For example, if the server is responding properly, you should see a status code of 200, which indicates a successful request.

Note that this assumes you have curl installed on your system. Also, ensure you replace the example URL with an appropriate URL for your use case.

To install curl on Linux

To install curl on Linux, you can use your distribution’s package manager. Here are some examples based on popular Linux distributions:

Ubuntu/Debian:

sudo apt-get update
sudo apt-get install curl

Fedora/RHEL/CentOS:

sudo yum install curl

OpenSUSE:

sudo zypper install curl

Once you have curl installed, you can use it to perform various operations, such as making HTTP requests, as we’ve discussed in the previous answer. To check if curl is installed, you can run the following command to get the version information:

curl --version

This will display the version of curl installed on your system. If curl is not installed, you can follow the appropriate installation steps for your distribution, as shown above.

Note that you may need to have administrative privileges (i.e. run the commands with sudo) to install packages on your system.

Tags: