Technotes

Technotes for future me

Curl

Check certificate

curl --insecure -v https://www.google.com 2>&1 | awk 'BEGIN { cert=0 } /^\* SSL connection/ { cert=1 } /^\*/ { if (cert) print }'

Connect via proxy

-x, –proxy <[protocol://][user:password@]proxyhost[:port]>
Use the specified HTTP proxy. If the port number is not specified, it is assumed at port 1080.

This option overrides existing environment variables that set the proxy to use. If there’s an environment variable setting a proxy, you can set proxy to "" to override it.

All operations that are performed over an HTTP proxy will transparently be converted to HTTP. It means that certain protocol specific operations might not be available. This is not the case if you can tunnel through the proxy, as one with the -p, –proxytunnel option.

User and password that might be provided in the proxy string are URL decoded by curl. This allows you to pass in special characters such as @ by using %40 or pass in a colon with %3a.

The proxy host can be specified the exact same way as the proxy environment variables, including the protocol prefix (http://) and the embedded user + password.

From 7.21.7, the proxy string may be specified with a protocol:// prefix to specify alternative proxy protocols. Use socks4://, socks4a://, socks5:// or socks5h:// to request the specific SOCKS version to be used. No protocol specified, http:// and all others will be treated as HTTP proxies.

If this option is used several times, the last one will be used.

curl -kx https://proxyhost:3128 https://icanhazip.com

Verbose

-k, –insecure
(SSL) This option explicitly allows curl to perform “insecure” SSL connections and transfers. All SSL connections are attempted to be made secure by using the CA certificate bundle installed by default.
This makes all connections considered “insecure” fail unless -k, –insecure is used.

curl -vk https://127.0.0.1:443 -H "Host: blaataap.com" -H "X-Forwarded-Proto: https"

Download

curl http://www.centos.org > centos-org.html

Follow HTTP Location Headers with -L option

curl -L http://www.google.com

Download a file only if it is modified before/after the given time

curl -z 21-Dec-11 http://www.example.com/yy.html

Pass HTTP Authentication in cURL

curl -u username:password URL

View only headers with Curl

curl -I -X GET https://technotes.adelerhof.eu

Provide hostname

curl -vk https://127.0.0.1:443 -H "Host: www.blaataap.com" -H "X-Forwarded-Proto: https"

Provide a custom address for a specific host and port pair

Using this, you can make the curl requests(s) use a specified address and prevent the otherwise normally resolved address to be used.
Consider it a sort of /etc/hosts alternative provided on the command line.
The port number should be the number used for the specific protocol the host will be used for.
It means you need several entries if you want to provide address for the same host but different ports.

curl --resolve blaataap.com:443:178.20.173.140 https://blaataap.com:443/whoami --insecure

Provide header

curl -vk https://127.0.0.1:443 -H "X-Forwarded-Proto: https"

https://en.wikipedia.org/wiki/List_of_HTTP_header_fields

Provide User Agent

curl -vk https://127.0.0.1:443 -H "User-Agent: user-Agent-Name-Here"

IPv6

curl -g -6 'http://[2a00:1d38:f8::6]:80/'

Login

curl --user user:pass --cookie-jar ./somefile https://xyz.com/login
curl --cookie ./somefile https://xyz.com/orders

timeout test

# Check blaataap.com certificate expiry
for url in blaataap.com
do
  echo "${url}"
  curl --insecure -vm 2 https://"${url}" 2>&1 | awk 'BEGIN { cert=0 } /^\* SSL connection/ { cert=1 } /^\*/ { if (cert) print }' | grep -e subject -e date

  res=$?
  if test "$res" != "0"; then
    echo "the curl command failed with timeout"
    read -n 1 -s -r -p "Press any key to continue"
    echo ""
  fi

done
Last updated on 19 Oct 2022
Published on 11 Dec 2019
Edit on GitHub