The --data
option (or -d
for short) in curl sends data in the body of an HTTP request. By default, it automatically converts the request method to POST and sets the Content-Type
header to application/x-www-form-urlencoded
.
Basic Syntax
curl --data "key=value" https://example.com/api
# or using the short form
curl -d "key=value" https://example.com/api
Form Data Examples
Simple Form Data
curl -d "username=john&password=secret" https://example.com/login
URL-encoded Special Characters
curl -d "message=Hello%20World%21&status=active" https://example.com/submit
Multiple Parameters
curl -d "name=John" -d "email=john@example.com" -d "age=30" https://example.com/users
JSON Data
When sending JSON, always set the Content-Type
header:
curl -d '{"name":"John","email":"john@example.com","age":30}' \
-H "Content-Type: application/json" \
https://example.com/api/users
JSON from File
curl -d @data.json -H "Content-Type: application/json" https://example.com/api
Data Variations
--data-raw
Prevents curl from interpreting @
and <
characters as file references:
curl --data-raw "email=user@domain.com" https://example.com/api
--data-urlencode
Automatically URL-encodes the data:
curl --data-urlencode "message=Hello World!" https://example.com/api
--data-binary
Sends data exactly as specified without processing:
curl --data-binary @image.jpg -H "Content-Type: image/jpeg" https://example.com/upload
Reading Data from Files
From Text File
curl -d @form-data.txt https://example.com/submit
From Standard Input
echo "username=admin&password=secret" | curl -d @- https://example.com/login
XML Data Example
curl -d '<?xml version="1.0"?><user><name>John</name><email>john@example.com</email></user>' \
-H "Content-Type: application/xml" \
https://example.com/api/users
Important Notes
- Automatic POST: Using
--data
automatically changes the HTTP method to POST - Content-Type: Defaults to
application/x-www-form-urlencoded
unless overridden - Special Characters: Use
--data-urlencode
for automatic URL encoding - File Safety: Use
--data-raw
to prevent file interpretation of@
symbols - Binary Data: Use
--data-binary
for exact data transmission without processing
Common Use Cases
- API Testing: Sending JSON payloads to REST APIs
- Form Submission: Automating web form submissions
- File Upload: Sending binary data or files to servers
- Authentication: Sending login credentials via POST requests