Skip to content

curl tricks

Timeout

curl -ks --max-time 6 --connect-timeout 3 <URL>

User-agent

CURL_USERAGENT="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36"
test -n "$CURL_USERAGENT" && curl_useragent="-A $CURL_USERAGENT"
curl $curl_useragent <URL>

proxy

As option

sock5

curl -x socks5://192.168.1.1:8888

http

curl -x http://192.168.1.1:8888

As env

export http_proxy="http://192.168.1.1:8888"
export https_proxy="http://192.168.1.1:8888"

curl <URL>

Send multi-line message by json

name="foo"

message="$@"
message="${message//$'\r'/}"  
message="${message//$'\n'/\\n}"

json="{'message':'${message}','name':'$name'}"
json=$(echo "$json" | tr "'" '"')

curl -s -X POST -H "Content-Type: application/json" "$URL" -d "$json"

Basic Authorization

U='YOUR-NAME'
P='YOUR-PASSWORD'
A=$(echo -ne "$U:$P"|base64 --wrap 0)

curl --header "Authorization: Basic $A" http://foo.com/

Get http code

RESPONSE=$(curl <URL> -w "\n%{response_code}" -o /dev/null)
HTTP_CODE="${RESPONSE: -3}"
[ "$HTTP_CODE" -eq 200 ] && { echo "SUCCESS";} || { echo "ERROR: $HTTP_CODE"; exit 1; }

Without DNS resolver

# ping domain.com: can't resolve
curl --resolve 'domain.com:443:11.22.33.44' https://domain.com/api/v1/test