Skip to content

Instantly share code, notes, and snippets.

@Rafe
Created December 6, 2015 18:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Rafe/960a7703ef41bfc19753 to your computer and use it in GitHub Desktop.
Save Rafe/960a7703ef41bfc19753 to your computer and use it in GitHub Desktop.
curl cheat sheet

Basic GET

Get a single resource via URI

Default operation is a GET:

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

Get multiple resources where IDs are in a range

Use square brackets with a dashed range:

curl http://api.example.com/items/[1000-2000]

GET multiple resources where IDs aren't in a range

Use curly braces with comma-delimited strings:

curl http://api.example.com/produxts/{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/items/1234

Add multiple headers

Use multiple -H options:

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

Show the network and HTTP 'conversation'

Use the verbose option: -v or --verbose

curl -v 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-from-urlencoded as the Content-Type, i.e., as if it was submitted by an HTML Form. You can use multiple -d optinos, which will be combined, e.g., these two commands produce the same content:

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

curl -d "first=Ted&last=Young" http://api.example.com/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/items

##PUT data to a URI

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

##Http authentication

curl -u username:password http://api.example.com/items

##Cookie

curl -b 'id:1234' http://example.com/items

##Limit rate

curl --limit-rate 10K www.far-away-site.com

##Ignore ssh

curl -i www.not-secure-test-site.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment