Curl#
#
Introduction#
Curl is a tool for transferring data between servers, supporting protocols, including:
HTTP
HTTPS
FTP
IMAP
LDAP
POP3
SCP
SFTP
SMB
SMTP
etc…
Curl GitHub source repository (github.com)
Curl Official Website (curl.se)
Options#
-o <file> # --output: write to file
-u user:pass # --user: authentication
-v # --verbose: Make curl verbose during operation
-vv # more verbose
-s # --silent: don't show progress meter or errors
-S # --show-error: When used with --silent (-sS), show errors but no progress meter
-i # --include: include HTTP headers in the output
-I # --head: header only
Request#
-X POST # --request
-L # If the page redirects, follow the link
-F # --form: HTTP POST data for multipart/form-data
data#
# --data: HTTP post data
# URL encoding (eg, status="Hello")
-d 'data'
# --data pass file
-d @file
# --get: send -d data via get
-G
Header information Headers#
-A <str> # --user-agent
-b name=val # --cookie
-b, --cookie FILE # Load cookies from the specified file for the URL
-c, --cookie-jar FILE # Save cookies to the specified file from the URL
-H "X-Foo: y" # --header
--compressed # use deflate/gzip
SSL#
--cacert <file>
--capath <dir>
-E, --cert <cert> # --cert: client certificate file
--cert-type # der/pem/eng
-k, --insecure # For self-signed certificates
Install#
apk add --update curl # install in alpine linux
Example#
CURL GET/HEAD#
command |
description |
|---|---|
|
|
|
|
|
use explicit http method for |
|
|
|
|
|
|
|
|
Multiple file upload#
$ curl -v --include \
--form key1=value1 \
--form upload=@localfilename URL
Prettify json output for curl response#
$ curl -XGET http://${elasticsearch_ip}:9200/_cluster/nodes | python -m json.tool
CURL POST#
command |
description |
|---|---|
|
|
|
|
CURL script install rvm#
curl -sSL https://get.rvm.io | bash
CURL Advanced#
command |
description |
|---|---|
|
get my public |
|
|
|
|
|
use http2 curl |
|
curl |
|
curl |
|
upload with credentials |
Check website response time#
curl -s -w \
'\nLookup time:\t%{time_namelookup}\nConnect time:\t%{time_connect}\nAppCon time:\t%{time_appconnect}\nRedirect time:\t%{time_redirect}\nPreXfer time:\t%{time_pretransfer }\nStartXfer time:\t%{time_starttransfer}\n\nTotal time:\t%{time_total}\n' \
-o /dev/null https://www.google.com
Use Curl to check if a remote resource is available#
curl -o /dev/null --silent -Iw "%{http_code}" https://example.com/my.remote.tarball.gz
Downloading file#
curl https://example.com | \
grep --only-matching 'src="[^"]*.[png]"' | \
cut -d \" -f2 | \
while read i; do curl https://example.com/"${i}" \
-o "${i##*/}"; done
Download all PNG files from the site (using GNU grep)
Download the file, save the file without changing its name#
curl --remote-name "https://example.com/linux-distro.iso"
Rename file
curl --remote-name "http://example.com/index.html" --output foo.html
continue partial download#
curl --remote-name --continue-at -"https://example.com/linux-distro.iso"
Download files from multiple domains#
curl "https://www.{example,w3,iana}.org/index.html" --output "file_#1.html"
Download a series of files#
curl "https://{foo,bar}.com/file_[1-4].webp" --output "#1_#2.webp"
Download a series of files (output foo_file1.webp, foo_file2.webp...bar_file1_webp, etc.)
Redirect output to file#
$ curl http://url/file > file
Basic Authentication#
$ curl --user username:password http://example.com/
$ curl -u username:password http://example.com/
Write to file instead of stdout#
$ curl -o file http://url/file
$ curl --output file http://url/file
Download header information#
$ curl -I url
# display header information
Write output to a file named remote_file#
$ curl -o file http://url/file
$ curl --output file http://url/file
Execute remote script#
$ curl -s http://url/myscript.sh
Configuration file#
curl -K file
# read configuration from file
curl --config file
$HOME/.curlrc # default configuration file on UNIX-like systems