Curl Command Examples - Transfer Data with Curl

Explore essential Curl command examples for transferring data from or to a server. Learn to download files, send POST requests, handle redirects, and more with this powerful command-line tool.

Curl Command Examples

Understanding Curl for Data Transfer

Curl is a powerful command-line tool used for transferring data to or from a server. It supports a wide range of protocols, including HTTP, HTTPS, FTP, and more. This makes it an indispensable utility for developers, system administrators, and anyone working with web services or network protocols. Below are common and practical examples of how to leverage Curl for various data transfer tasks.

Common Curl Operations

Downloading Files with Curl

You can download files from a URL and save them with a specific name or the original filename.

# Download a file from a URL and save it with a specific name
curl -o filename.ext http://example.com/file.txt

# Download a file from a URL and save it with the original filename
curl -O http://example.com/file.txt

# Resume a failed or interrupted download
curl -C - -O http://example.com/largefile.zip

Sending Data with Curl

Curl is frequently used to send data to servers, especially for API interactions.

# Send POST data to a server
curl -d "name=value" http://example.com/resource

# Send JSON data with a POST request
curl -H "Content-Type: application/json" -d '{"key":"value"}' http://example.com/resource

# Send data with the URL encoded format
curl --data-urlencode "key=value" http://example.com/resource

# Use a different request method like PUT or DELETE
curl -X PUT http://example.com/resource

Handling HTTP Requests and Headers

Inspect and manipulate HTTP headers for detailed request and response analysis.

# Follow redirects if the URL has moved
curl -L http://example.com

# Include headers in the output
curl -i http://example.com

# Display only the HTTP headers for a GET request
curl -I http://example.com

# Send a request with a custom header
curl -H "Custom-Header: Value" http://example.com

# Save the response headers to a file
curl -D headers.txt http://example.com

Advanced Curl Usage

Explore more specialized functionalities for complex scenarios.

# Download a file and limit the download speed
curl --limit-rate 100K http://example.com/file.txt

# Authenticate with a username and password
curl -u username:password http://example.com

# Download multiple URLs in sequence
curl -O http://example.com/file1.txt -O http://example.com/file2.txt

# Transfer a file using FTP
curl -T localfile.txt ftp://ftp.example.com/upload/

# Specify a proxy for the request
curl -x http://proxy-server:port http://example.com

# Download a file and run it through a pipe (e.g., to `grep`)
curl http://example.com/file.txt | grep "search-string"

Further Resources

For more in-depth information and advanced usage, consult the official Curl documentation and related resources: