Skip to content

Instantly share code, notes, and snippets.

@asanchezr
Last active June 13, 2021 04:07
Show Gist options
  • Save asanchezr/19e04c23c1eebce6e5218e4d2acbb17b to your computer and use it in GitHub Desktop.
Save asanchezr/19e04c23c1eebce6e5218e4d2acbb17b to your computer and use it in GitHub Desktop.
Curl Cheatsheet

Curl Cheatsheet

TL;DR

XML GET

curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET "http://hostname/resource"

JSON GET

curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET "http://hostname/resource"

JSON PUT

curl -i -H 'Content-Type: application/json' -H 'Accept: application/json' -X PUT -d '{"updated_field1":"updated_value1"}' "http://hostname/resourcex"

JSON POST uploading a file

curl -i -H 'Accept: application/json' -X POST -F "filename=@/file/path" -F "other_field=its_value"   "http://hostname/resource"

JSON DELETE

curl -i -H 'Content-Type: application/json' -H 'Accept: application/json' -X DELETE -d '{"key1":"value1"}' "http://hostname/resource"

"Debugging mode" (without actual content output):

curl -XGET -vvv http://hostname/resource > dev\null

Useful arguments

  • -k: not check SSL certificates
  • -L: follow redirects
  • -v: get verbose output
  • -V: get headers at output

Basic GET

GET a single resource via its URI

Default operation is a GET:

curl http://api.example.com:8080/statuses/1234

GET multiple resources where IDs are in a range

Use square brackets with a dashed range:

curl http://api.example.com:8080/items/[1230-1234]

GET multiple resources where IDs aren't in a range

Use curly braces with comma-delimited strings:

curl http://api.example.com:8080/products/{abc,def,ghi}/status

Using HTTP Headers

Accept only the application/json content-type

Use the header option: -H or --header

curl -H 'Accept: application/json' http://api.example.com:8080/items/1234

Add multiple headers

Use multiple -H options:

curl -H 'Accept: application/json' -H 'Accept-Encoding: gzip' http://api.example.com:8080/products/a1b2c3ef/status

Note: the output from this is likely to be unreadable, because it's gzipped!

More likely you'd use this with ETags:

curl -H 'Accept: application/json' -H 'If-None-Match: "1cc044-172-3d9aee80"' 
        http://api.example.com:8080/products/a1b2c3ef/status

Show the network and HTTP "conversation"

Use the verbose option: -v or --verbose

curl -v -H http://api.example.com

POST and PUT

POST data to a URI

To send data to the server, you use either POST or PUT, depending on what the API requires. To do a POST, you simply use the -d (--data) with some content:

curl -d "name=Ted" http://api.example.com:8080/customers

Note that this uses application/x-www-form-urlencoded as the Content-Type, i.e., as if it was submitted by an HTML Form. You can use multiple -d options, which will be combined, e.g., these two commands produce the same content:

curl -d "first=Ted" -d "last=Young" http://api.example.com:8080/customers
curl -d "first=Ted&last=Young" http://api.example.com:8080/customers

If you want to send JSON, you'll need to specify the Content-Type explicitly using the -H (header) option:

curl -d '{"name": "Ted"}' -H 'Content-Type: application/json' http://api.example.com:8080/items

PUT data to a URI

If you need to use the PUT method, you'll need to override the method with the -X (--request) option:

curl -X PUT -d '{"name": "Ted"}' -H 'Content-Type: application/json' http://api.example.com:8080/items/1234

Examples...

POST a file

curl --data-binary @/path/to/file <url>

By adding --data-binary, curl will change the method to POST and set the Content-Type header to application/x-www-form-urlencoded.

POST image file

curl -X POST -H 'Content-Type: image/jpeg' -T pictures/car.jpg <url>

Add Request Header

-H '<Header-Name>: <Header-Value>'

Change HTTP method

-X GET|PUT|POST|DELETE|OPTIONS

Verbose mode

See what curl is actually doing:

curl -v

Dump response header data to stdout

curl -D -

Do a GET, but ignoring the data

On unix:

curl -o /dev/null <other-options> <url>

On Windows:

curl -o nul <other-options> <url>

This is useful if combined with -D to give you the headers only:

curl -D - -o /dev/null <url>

Add If-None-Match

This header is mostly useful on conditional GET requests:

-H 'If-None-Match: <value-of-etag-header>'

Add If-Match

This header is mostly useful on conditional PUT|POST requests:

-H 'If-Match: <value-of-etag-header>'

Add If-Modified-Since

This header is mostly useful on conditional GET requests:

-H 'If-Modified-Since: <value-of-last-modified-header>'

Add If-Unmodified-Since

This header is mostly useful on conditional PUT|POST requests:

-H 'If-Unmodified-Since: <value-of-last-modified-header>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment