You’ve undoubtedly heard of curl, as it’s one of the most popular HTTP utilities used on Linux and other unix-based systems. You may not know that it is able to transfer data for a rather generous list of protocols (you can find the whole list at the cURL website). It’s also a widely used library in software applications, but today we’re going to stick to the command line utility.

Below are a list of cURL showing common use cases you’ll encounter. I’ve preferred to use the long-style argument names as I feel they’re easier to remember.

One note, you’ll see a lot of examples for POST that involve using a flag like -X POST. As also noted in the official cURL man page, this only changes the method name in the request, it does not change cURL’s behavior. You’re usually better-off using options like --data to POST, and --head to make HEAD requests, as -X,--request can provide unexpected results:

This option only changes the actual word used in the HTTP request, it does not alter the way curl behaves. So for example if you want to make a proper HEAD request, using -X HEAD will not suffice. You need to use the -I, –head option.

The method string you set with -X, –request will be used for all requests, which if you for example use -L, –location may cause unintended side-effects when curl doesn’t change request method according to the HTTP 30x response codes - and similar.

GET Examples

show response body:

curl google.com

show response headers and body:

curl --include google.com

show only response headers:

curl --head google.com

don’t show progress meter or errors:

curl --silent ftp.ucsb.edu/ubuntu.iso

follow redirects:

curl --location google.com

skip SSL certificate verification:

curl --insecure https://10.0.0.1

use a specific network interface:

curl --interface eth0 google.com

force IPv4:

curl --ipv4 yahoo.com

force IPv6:

curl --ipv6 ipv6.google.com

basic authentication:

curl --user=michael:password123 mrod.space/admin_stats

save response to a file of the same name (like wget):

curl -O example.com/test.txt

save response to a specified filename:

curl -o new_name.txt example.com/test.txt

send URL-encoded key/value pair options in the URL:

curl --get --data-urlencode key0=value0 --data-urlencode key1=value1 example.com

POST Examples

JSON payload with custom headers:

curl --header "Content-type: application/json" --data '{"key":"value"}' example.com:8080

load JSON payload from a file:

curl --header "Content-type: application/json" --data @/home/michael/json.txt example.com:8080

URL-encoded key/value pair options:

curl --data-urlencode key0=value0 --data-urlencode key1=value1 example.com:8080