antonio
antonio

Reputation: 11130

cURL: manage download errors

I tried to wrote a Bash script to manage cURL download errors by parsing the response header. However, with all Github downloads, after the redirect the header is HTTP/1.1 403 Forbidden, despite the fact that the download works.

function curldown {

    url="$1"
    code=$(curl -LI $url | awk '/^HTTP/{a=$2} END{print a}')

    if [[ "${code:0:1}"  == "4" ]]; then
    echo "Error $code"
    else
    curl -JOL $url > /tmp/curldown
    fi

}

url='https://github.com/keybase/client/releases/download/v5.1.1/keybase-v5.1.1.tar.xz'
curldown $url
# Error 403

but

curl -JOL $url

gives a working output.

Any fix?

Upvotes: 0

Views: 163

Answers (1)

Gilles Quénot
Gilles Quénot

Reputation: 185610

A better solution :

http_code=$(curl -Ls "$url" -w '%{http_code}\n' -o /dev/null)

if ((http_code >= 400)); then
    echo >&2 "Error detected; HTTP code $http_code"
fi

Upvotes: 1

Related Questions