You’ve undoubtedly heard of curl, as it’s one of the most popular HTTP utilities used on Linux and uther 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.

cURL Examples

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

  • GET a page, show response body:
    curl google.com
    
  • GET a page, show headers and response body:
    curl --include google.com
    
  • GET a page, show just the headers:
    curl --head google.com
    
  • GET a file, don’t show progress meter or errors:
    curl --silent ftp.ucsb.edu/ubuntu.iso
    
  • GET a page, following redirects, show only the response body:
    curl --location google.com
    
  • GET a page over HTTPS w/o verifying the certificate:
    curl --insecure https://10.0.0.1
    
  • GET a page using a specific network interface:
    curl --interface eth0 google.com
    
  • GET a page, only resolving IPv4 addresses:
    curl --ipv4 yahoo.com
    
  • GET a page, only resolving IPv6 addresses:
    curl --ipv6 ipv6.google.com
    
  • GET a page silently, without printing errors or progress:
    curl --silent google.com
    
  • GET a page that requires basic authentication:
    curl --user=michael:password123 mrod.space/admin_stats
    
  • GET a page and save it to a file of the same name (like wget):
    curl -O example.com/test.txt
    
  • GET a page and save it to a specified filename:
    curl -o new_name.txt example.com/test.txt
    
  • GET a page with URL-encoded key/value pair options in the URL:
    curl --get --data-urlencode key0=value0 --data-urlencode key1=value1 example.com
    

POST Examples

  • POST JSON to an endpoint and specify the header:
    curl --header "Content-type: application/json" --data '{"key":"value"}' example.com:8080
    
  • POST JSON from a file to an endpoint and specify the header:
    curl --header "Content-type: application/json" --data @/home/michael/json.txt example.com:8080
    
  • POST URL-encoded key/value pair options to an endpoint:
    curl --data-urlencode key0=value0 --data-urlencode key1=value1 example.com:8080