In cURL, -d and --data are actually exactly the same parameter, just different short and long forms. However, cURL provides multiple ways to send data for different scenarios.
Difference Between -d and --data
bash# Both are completely equivalent curl -d "name=value" https://api.example.com curl --data "name=value" https://api.example.com
Data Sending Methods Comparison
| Parameter | Purpose | Content-Type | Example |
|---|---|---|---|
-d / --data | Send data (default URL-encoded) | application/x-www-form-urlencoded | -d "name=value" |
--data-ascii | Send ASCII text | application/x-www-form-urlencoded | --data-ascii "text" |
--data-binary | Send binary data | Not set | --data-binary @file.bin |
--data-urlencode | URL-encoded data | application/x-www-form-urlencoded | --data-urlencode "name=value" |
--data-raw | Send as-is (no @ processing) | application/x-www-form-urlencoded | --data-raw "@username" |
Detailed Usage Examples
bash# 1. Basic data sending (URL-encoded) curl -X POST https://api.example.com/login \ -d "username=admin&password=123456" # 2. Send JSON data (need to manually set Content-Type) curl -X POST https://api.example.com/users \ -H "Content-Type: application/json" \ -d '{"name":"John","age":25}' # 3. Read data from file curl -X POST https://api.example.com/upload \ -d @data.json # 4. Send binary file curl -X POST https://api.example.com/upload \ -H "Content-Type: application/octet-stream" \ --data-binary @/path/to/file.bin # 5. URL-encode special characters curl -X POST https://api.example.com/search \ --data-urlencode "query=hello world!" # Result: query=hello%20world%21 # 6. Send as-is (don't process @ symbol) curl -X POST https://api.example.com/webhook \ --data-raw "@channel message here"
Multiple Data Segments
bash# Send multiple data segments curl -X POST https://api.example.com/form \ -d "name=John" \ -d "email=john@example.com" \ -d "age=25" # Mixed usage curl -X POST https://api.example.com/mixed \ -d "title=Test" \ --data-urlencode "content=Hello World!" \ --data-binary @attachment.pdf
Different Data Format Examples
bash# Form data (application/x-www-form-urlencoded) curl -X POST https://api.example.com/form \ -H "Content-Type: application/x-www-form-urlencoded" \ -d "field1=value1&field2=value2" # JSON data (application/json) curl -X POST https://api.example.com/api \ -H "Content-Type: application/json" \ -d '{"key":"value","array":[1,2,3]}' # XML data (application/xml) curl -X POST https://api.example.com/soap \ -H "Content-Type: application/xml" \ -d '<?xml version="1.0"?><root><item>value</item></root>' # Plain text (text/plain) curl -X POST https://api.example.com/log \ -H "Content-Type: text/plain" \ --data-binary @log.txt
Important Notes
- Default behavior:
-dautomatically setsContent-Type: application/x-www-form-urlencoded - Override Content-Type: For JSON, must manually set
-H "Content-Type: application/json" - File reading:
-d @filenamereads content from file - @ symbol handling:
--data-rawprevents@from being interpreted as file path - URL encoding:
--data-urlencodeautomatically encodes special characters
Practical Tips
bash# Send complex JSON (using heredoc) curl -X POST https://api.example.com/users \ -H "Content-Type: application/json" \ -d @- <<'EOF' { "user": { "name": "John", "email": "john@example.com", "roles": ["admin", "user"] }, "settings": { "theme": "dark", "notifications": true } } EOF # Send Base64-encoded data curl -X POST https://api.example.com/upload \ -H "Content-Type: application/json" \ -d "{\"file\":\"$(base64 -w 0 image.png)\"}"