Skip to content

Instantly share code, notes, and snippets.

@anyu
Last active July 7, 2021 19:30
Show Gist options
  • Save anyu/fecfa3bebe87d8c59c6c04566136dcaf to your computer and use it in GitHub Desktop.
Save anyu/fecfa3bebe87d8c59c6c04566136dcaf to your computer and use it in GitHub Desktop.
curl_cheatsheet

cURL cheatsheet

Core flags

Flag Description
-H, --header 'name: value' Send request headers
-d, --data Send request data
-X, --request The request method. -X POST is implied if -d is used.
-b, --cookie 'SOME-COOKIE=val1;OTHER-COOKIE=val2' Send request cookies
-c, --cookie-jar <filename> Save response cookies to file

Commonly useful flags

Flag Description
-i, --include Include HTTP response headers in output
-O, --remote-name Download file
-o, --output <file> Write output to specific location
-L, --location Follow redirects
-s, --silent Quiet mode
-v, --verbose Output debugging info

POST request

Send form-encoded data

This is the default format if a different Content-Type is not specified. Provided data must be URL-encoded1

The -H 'Content-Type: application/x-www-form-urlencoded' header is implied and does not need to be provided.

curl -d 'email=bob%40gmail.com&password=somepass' http://example.com

Data can also be passed individually. Curl will automatically concatenate them with ampersands.

curl -d 'email=bob%40gmail.com' -d 'password=somepass' http://example.com

Data can be passed in a file

curl -d '@data.txt' http://example.com

Data that's not URL encoded can be encoded for you

curl --data-urlencode '[email protected]' http://example.com    # gets converted to 'email=bob%40gmail.com'

Nested data

# Pre URL-encoded data string
# tracking={"key1":"value1","key2":"value2"}

curl -d 'tracking%3D%7B%22key1%22%3A%22value1%22%2C%22key2%22%3A%22value2%22%7D%0A%0A' \
http://example.com

More complicated nested data

# Pre URL-encoded data string
# tracking={"tags": ["tag1", "tag2"],
	"product": "some-product",
	"categories": [{
		"id": "100",
		"type": "type1"
	}, {
		"id": "101",
		"type": "type2"
	}]}


curl -d 'tracking%3D%7B%22tags%22%3A%20%5B%22tag1%22%2C%20%22tag2%22%5D%2C%0A%09%22product%22%3A%20%22some-product%22%2C%0A%09%22categories%22%3A%20%5B%7B%0A%09%09%22id%22%3A%20%22100%22%2C%0A%09%09%22type%22%3A%20%22type1%22%0A%09%7D%2C%20%7B%0A%09%09%22id%22%3A%20%22101%22%2C%0A%09%09%22type%22%3A%20%22type2%22%0A%09%7D%5D%7D%0A' \
http://example.com

Send JSON formatted data

curl -d '{"email":"[email protected]", "password":"somepass"}' -H 'Content-Type: application/json' http://example.com

Data can be passed in a file

curl -d '@data.json' http://example.com

Nested data (probably easier to pass in via a file)

# Multi-line nested JSON data
{
	"tags": ["tag1", "tag2"],
	"product": "some-product",
	"categories": [{
		"id": "100",
		"type": "type1"
	}, {
		"id": "101",
		"type": "type2"
	}]
}

curl -H 'Content-Type: application/json' \
-d '{"tags":["tag1","tag2"],"product":"some-product","categories":[{"id":"100","type":"type1"},{"id":"101","type":"type2"}]}' \
http://example.com

1: URL encoding (aka. Percent encoding) converts characters into ASCII characters. Unsafe ASCII characters are replaced with a "%" followed by two hexadecimal digits.

@anyu
Copy link
Author

anyu commented Feb 21, 2021

TODO: Replace urls with httpbin.org

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