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 |
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 |
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
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.
TODO: Replace urls with httpbin.org