乐闻世界logo
搜索文章和话题

What is the difference between -d and --data in cURL? How to send different data formats?

3月6日 21:48

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

ParameterPurposeContent-TypeExample
-d / --dataSend data (default URL-encoded)application/x-www-form-urlencoded-d "name=value"
--data-asciiSend ASCII textapplication/x-www-form-urlencoded--data-ascii "text"
--data-binarySend binary dataNot set--data-binary @file.bin
--data-urlencodeURL-encoded dataapplication/x-www-form-urlencoded--data-urlencode "name=value"
--data-rawSend 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

  1. Default behavior: -d automatically sets Content-Type: application/x-www-form-urlencoded
  2. Override Content-Type: For JSON, must manually set -H "Content-Type: application/json"
  3. File reading: -d @filename reads content from file
  4. @ symbol handling: --data-raw prevents @ from being interpreted as file path
  5. URL encoding: --data-urlencode automatically 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)\"}"

标签:cURL