Table of contents

What does the --data option do in Curl?

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

Try WebScraping.AI for Your Web Scraping Needs

Looking for a powerful web scraping solution? WebScraping.AI provides an LLM-powered API that combines Chromium JavaScript rendering with rotating proxies for reliable data extraction.

Key Features:

  • AI-powered extraction: Ask questions about web pages or extract structured data fields
  • JavaScript rendering: Full Chromium browser support for dynamic content
  • Rotating proxies: Datacenter and residential proxies from multiple countries
  • Easy integration: Simple REST API with SDKs for Python, Ruby, PHP, and more
  • Reliable & scalable: Built for developers who need consistent results

Getting Started:

Get page content with AI analysis:

curl "https://api.webscraping.ai/ai/question?url=https://example.com&question=What is the main topic?&api_key=YOUR_API_KEY"

Extract structured data:

curl "https://api.webscraping.ai/ai/fields?url=https://example.com&fields[title]=Page title&fields[price]=Product price&api_key=YOUR_API_KEY"

Try in request builder

Related Questions

Get Started Now

WebScraping.AI provides rotating proxies, Chromium rendering and built-in HTML parser for web scraping
Icon