Curl Tips and Tricks

Fizzle (-fsSL)

  • -f, --fail: fail silently on server errors
  • -s, --silent: mute curl progress and other output
  • -S, --show-error: when used with --silent, print error message
  • -L, --location: follow redirects (3xx)

Extract tar archive

curl -fsSL https://server.com/drive/project.tar.gz | tar xzv [-C /my/directory/]

Run script

curl -fsSL https://server.com/myscript.sh | bash

Get the server response code

-o /dev/null : don't show any output
-w, --write-out <format>
       Defines  what  to  display  on  stdout after a completed and successful operation. The format is a string that may contain plain text mixed with any number of variables.
  •   curl -s -o /dev/null -w '%{http_code}' https://github.com
      200
    
  • # -L: follow the redirect
    # -I: get headers
    curl -LIs -o /dev/null -w '%{http_code}' https://github.com
    

Get the response time

curl -o /dev/null -s -w 'Total: %{time_total}s\n' https://github.com
Total: 0.604334s

Download Files

Keep the file name same as the resource (-O, --remote-name)

curl -O https://server.com/resource.mp4

Save with a different name (-o, --output)

curl -o myfile.mp4 https://server.com/resource.mp4

Download multiple files

All resources inline

curl -O https://server.com/resource1.pdf -O https://server.com/resource2.pdf

All resources from the file

xargs -n 1 curl -O < myresources.txt

Continue download should session breaks

curl -C - https://server.com/resource.mp4

SSL

Specify ssl certificate (--cacert)

curl --cacert cert.pem https://secure-server.com/resource

Disable ssl check (-k, --insecure)

curl -k https://secure-server.com/resource

Rest API Calls

  • -H "key:value" (--header)
  • -X [GET|POST|PUT|DELETE|PATCH] (--request)
  • -d '{json-data}' (--data)
  • --data-binary @filepath

Fetch resource (GET)

❯ curl https://reqres.in/api/users\?page\=1
{"page":1,"per_page":6,"total":12,"total_pages":2,"data":[{"id":1,"email":"george.bluth@reqres.in","first_name":"George","last_name":"Bluth","avatar":"https://reqres.in/img/faces/1-image.jpg"},{"id":2,"email":"janet.weaver@reqres.in","first_name":"Janet","last_name":"Weaver","avatar":"https://reqres.in/img/faces/2-image.jpg"},{"id":3,"email":"emma.wong@reqres.in","first_name":"Emma","last_name":"Wong","avatar":"https://reqres.in/img/faces/3-image.jpg"},{"id":4,"email":"eve.holt@reqres.in","first_name":"Eve","last_name":"Holt","avatar":"https://reqres.in/img/faces/4-image.jpg"},{"id":5,"email":"charles.morris@reqres.in","first_name":"Charles","last_name":"Morris","avatar":"https://reqres.in/img/faces/5-image.jpg"},{"id":6,"email":"tracey.ramos@reqres.in","first_name":"Tracey","last_name":"Ramos","avatar":"https://reqres.in/img/faces/6-image.jpg"}],"support":{"url":"https://reqres.in/#support-heading","text":"To keep ReqRes free, contributions towards server costs are appreciated!"}}

Create resource (POST)

❯ curl --data \         
'{
    "name": "morpheus",
    "job": "leader"
}' \
-H "Content-Type: application/json" \
-X POST "https://reqres.in/api/users"     
{"name":"morpheus","job":"leader","id":"508","createdAt":"2022-09-04T05:47:58.527Z"}

Update resource (PUT)

❯ curl --data \                                 
'{
    "name": "morpheus",
    "job": "zion resident"
}' \
-H "Content-Type: application/json" \
-X PUT "https://reqres.in/api/users/2"  
{"name":"morpheus","job":"zion resident","updatedAt":"2022-09-04T05:48:35.310Z"}

Delete resource (DELETE)

❯ curl -X DELETE "https://reqres.in/api/users/2"

Upload a file (POST)

❯ curl --data-binary @pexels-kevin-ku-577585.jpg \
-H "Content-Type: image/jpeg" \      
-H "Authorization: Bearer public_12a1xpA7xb5gMBrYSN2RVp5Q" \
-X POST "https://api.upload.io/v1/files/basic"
{"accountId":"12a1xpA","fileId":"12a1xpAY6NfbHmb9CZDw4RN","fileUniquifier":"Y6NfbHmb9CZDw4RN","fileUrl":"https://upcdn.io/12a1xpAY6NfbHmb9CZDw4RN"}