What’s the exit code for “curl -I” when not HTTP 200

curlexit codehtmlhttp-status-codespython

I want to check what HTTP status code is returned for an HTTP(S) URL. I don't care about content, so I just request head with
curl -I $url
or
curl –head $url

But what's the exit code I should check for, e.g. in subprocess.check_call? In particular, do I get a non-zero exit code for HTTP 403?

Best Answer

curl -I will always return 0, if it managed to produce an output with the HEAD. You have two alternatives.

The first is to use curl -I --fail instead, and check for exit code 22.

If you're doing this in a Python script, it could look like:

try:
    subprocess.check_call(['curl', '-I', '--fail', url])
except subprocess.CalledProcessError as e:
    if e.returncode == 22:
        (do something)

The second is to actually ask just the HTTP status code, like this:

$ curl -s -I -o /dev/null -w '%{http_code}' $bad-url
403