Link Search Menu Expand Document

curl Cheat Sheet


 curl -k           # Ignore SSL errors
 curl -s           # Silence curl download infos, just content
 curl -X PUT --data-binary @myinput.json     # use data from file
 # POST with data
 curl -XPOST --data-binary @myinput.txt
 curl -d '{ "key": "value", "some": "json" }'
 curl -d 'param=key&param=key'           # application/x-www-form-urlencoded
 # HEAD request
 curl -I


 -m <seconds>                  # Maximum timeout for entire request
 --connect-timeout <seconds>   # Maximum connect timeout

Secure Authentication with curl

Don’t do insecure auth

These credential passing methods might be insecure (depending on your use case) as they expose the password in the process list.

  1. Via simple basic auth via URL (i.e. https://<user>:<pwd>@<server>)
  2. Via -u "<user>:<pwd>"

Use piped config

echo '-u "<user>:<pwd>"' | curl -K "" -K-

Use .netrc

 # Variants with .netrc
 curl -n ""                 # Mandatory auth via .netrc
 curl --netrc-optional ""   # Optional auth via .netrc
 # On-the-fly .netrc
 curl --netrc-file <(echo " <user> <pwd>") ""

Using curl metrics

You can use curl to measure resolving, time to connect, time to first byte and total time like this:

  • Issue sequential requests (each with new connection)

    while true; do
       curl -w "$(date +%FT%T)    dns %{time_namelookup}    connect %{time_connect}   firstbyte %{time_starttransfer}   total %{time_total}   HTTP %{http_code}\n" -o /dev/null -s ""
       sleep 1
  • Issue sequential HTTP/1.1 requests on 1 connection

    curl -w "$(date +%FT%T)    dns %{time_namelookup}    connect %{time_connect}   firstbyte %{time_starttransfer}   total %{time_total}   HTTP %{http_code}\n" --keepalive -K <(printf 'url=""\n%.0s' {1..10000})