Skip to content

Curl

curl is a versatile command-line tool used to transfer data to or from a server.

It supports various protocols, such as HTTP, HTTPS, FTP, and more.


Basic Usage

Get a URL's Content

curl https://example.com

This fetches the HTML content of the page at the specified URL and prints it to the terminal.

Note

This command outputs the response directly to the terminal. Use the -o or -O options to save it to a file.

Download a File

curl -O https://example.com/file.txt

The -O option saves the file with its original name.

Save to a Specific File

curl -o myfile.txt https://example.com/file.txt

The -o option lets you specify the name for the saved file.


Sending Data

Send a GET Request with Parameters

curl "https://example.com?key1=value1&key2=value2"

Info

URL parameters are included directly in the query string.

Send a POST Request

curl -X POST -d "key1=value1&key2=value2" https://example.com
  • -X POST: Specifies the HTTP method.
  • -d: Sends data in the body of the request.

Warning

Be cautious when sending sensitive data in plain text. Use HTTPS to secure your request.

Send JSON Data

curl -X POST -H "Content-Type: application/json" -d '{"key1":"value1"}' https://example.com
  • -H: Adds headers to the request.

Warning

Many APIs require JSON data. Ensure the Content-Type header is set appropriately.


Authentication

Basic Authentication

curl -u username:password https://example.com
  • -u: Adds a username and password.

Danger

Avoid including sensitive credentials directly in the command, as they may be stored in shell history. Use environment variables for better security.

Bearer Token Authentication

curl -H "Authorization: Bearer your_token" https://example.com

Headers and Responses

Include Response Headers

curl -i https://example.com

Custom Request Headers

curl -H "Custom-Header: Value" https://example.com

Example

Custom headers can be useful for testing APIs or adding metadata to your requests.


Advanced Features

Follow Redirects

curl -L https://example.com

Note

Use the -L option to handle HTTP redirects automatically.

Limit the Rate of Data Transfer

curl --limit-rate 100k https://example.com/file.txt

Info

This is helpful for testing bandwidth limits or ensuring your server isn't overloaded.

Upload a File

curl -T myfile.txt https://example.com/upload

Example

Some servers may require authentication or specific headers for file uploads.

Save Cookies

curl -c cookies.txt https://example.com

Use Cookies

curl -b cookies.txt https://example.com

Tip

Saved cookies are often required to maintain sessions with websites.


Debugging

Verbose Mode

curl -v https://example.com

Info

Use verbose mode to see detailed information about the request and response, including headers and connection details.

Show Only Response Code

curl -o /dev/null -s -w "%{http_code}\n" https://example.com

Note

This is useful for monitoring server responses or scripting health checks.


Helpful Tips

  • Use man curl for the official manual.
  • Add --help to see a list of options:
    curl --help