# Cheatsheet & Examples: curl

## Download a webpage

Example Usage:
`curl example.com`

What it does:
Fetches the content of the webpage at `example.com` and displays it in the terminal.

Command-line Arguments Explained:

- `example.com`: The URL to fetch the content from.

---

## Upload data via POST

Example Usage:
`curl -X POST -d "data=example" http://example.com/api`

What it does:
Sends data to the specified URL using the HTTP POST method.

Command-line Arguments Explained:

- `-X POST`: Specifies the HTTP request method as POST.
- `-d "data=example"`: Sends the given data in the request body (used with POST/PUT).

---

## Send custom headers

Example Usage:
`curl -H "User-Agent: Mozilla" -H "Accept: application/json" example.com`

What it does:
Adds custom HTTP headers to the request (e.g., User-Agent, Accept).

Command-line Arguments Explained:

- `-H "Header: Value"`: Specifies a custom HTTP header to include in the request.

---

## Follow redirects automatically

Example Usage:
`curl -L http://short.url`

What it does:
Automatically follows HTTP redirects (e.g., 301, 302) to the final destination.

Command-line Arguments Explained:

- `-L`: Tells curl to follow redirects.

---

## Save output to a file

Example Usage:
`curl -o output.html example.com`

What it does:
Saves the fetched content to a file named `output.html` instead of printing it to the terminal.

Command-line Arguments Explained:

- `-o output.html`: Specifies the filename to save the output to.

---

## Retrieve HTTP headers only

Example Usage:
`curl -I example.com`

What it does:
Fetches the HTTP headers of the specified URL without the body content.

Command-line Arguments Explained:

- `-I`: Outputs only the HTTP headers.

---

## Use a proxy server

Example Usage:
`curl -x http://proxy.example.com:8080 example.com`

What it does:
Routes the request through a specified proxy server.

Command-line Arguments Explained:

- `-x http://proxy.example.com:8080`: Sets the proxy server to use for the request.

---

## Send cookies with a request

Example Usage:
`curl -b cookies.txt example.com`

What it does:
Includes cookies from the `cookies.txt` file in the request.

Command-line Arguments Explained:

- `-b cookies.txt`: Sends cookies stored in the specified file with the request.

---

## Retrieve HTTP status code

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

What it does:
Prints the HTTP status code (e.g., 200, 404) of the response without the body.

Command-line Arguments Explained:

- `-w "%{http_code}\n"`: Formats the output to show only the HTTP status code.
- `-o /dev/null`: Discards the response body by writing it to `/dev/null`.
- `-s`: Suppresses progress output.

---

## Download a file with the same name

Example Usage:
`curl -O http://example.com/file.txt`

What it does:
Downloads the file at `http://example.com/file.txt` and saves it locally with the same name.

Command-line Arguments Explained:

- `-O`: Saves the file using the remote filename.

---

## Upload a file via PUT

Example Usage:
`curl -X PUT -d @file.txt http://example.com/upload`

What it does:
Uploads the contents of `file.txt` to the specified URL using the PUT method.

Command-line Arguments Explained:

- `-X PUT`: Specifies the HTTP request method as PUT.
- `-d @file.txt`: Reads the request body from the local file `file.txt`.

---

## Basic HTTP authentication

Example Usage:
`curl -u username:password http://example.com/api`

What it does:
Sends HTTP Basic Authentication credentials in the request headers.

Command-line Arguments Explained:

- `-u username:password`: Provides the username and password for authentication.

---

## Check connection time (time to first byte)

Example Usage:
`curl -w "%{time_connect}\n" -s http://example.com`

What it does:
Displays the time taken for the TCP connection to be established.

Command-line Arguments Explained:

- `-w "%{time_connect}\n"`: Customizes the output to show the time to connect.

---

## Send JSON data in a POST request

Example Usage:
`curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' http://example.com/api`

What it does:
Sends a JSON payload to an API endpoint with the correct content type.

Command-line Arguments Explained:

- `-H "Content-Type: application/json"`: Sets the request header to indicate JSON content.
- `-d '{"key":"value"}'`: Sends the JSON data in the request body.

---

## Download multiple files

Example Usage:
`curl -O http://example.com/file1.txt -O http://example.com/file2.txt`

What it does:
Downloads multiple files from the specified URLs.

Command-line Arguments Explained:

- `-O`: Saves each file with its remote filename.

---

## Simulate a HEAD request

Example Usage:
`curl -X HEAD http://example.com`

What it does:
Sends a HEAD request to retrieve headers without the response body.

Command-line Arguments Explained:

- `-X HEAD`: Specifies the HTTP request method as HEAD.

---

## Stream output to a file

Example Usage:
`curl -OJ http://example.com/file.txt`

What it does:
Downloads the file and preserves its original filename and content disposition.

Command-line Arguments Explained:

- `-O`: Saves the file with the remote name.
- `-J`: Uses the filename from the Content-Disposition header.

---

## Send form-encoded data

Example Usage:
`curl -X POST -d "username=admin&password=secret" http://example.com/login`

What it does:
Submits form data (application/x-www-form-urlencoded) to a login endpoint.

Command-line Arguments Explained:

- `-d "username=admin&password=secret"`: Sends form-encoded data in the request body.

---

## Use a specific user agent

Example Usage:
`curl -A "MobileUserAgent" example.com`

What it does:
Sets the User-Agent string to mimic a mobile browser.

Command-line Arguments Explained:

- `-A "MobileUserAgent"`: Sets the User-Agent header for the request.

---

## Show verbose output

Example Usage:
`curl -v example.com`

What it does:
Displays detailed information about the request and response, including headers and timing.

Command-line Arguments Explained:

- `-v`: Enables verbose mode for debugging purposes.

---

## Resume a partially downloaded file

Example Usage:
`curl -C - -o file.txt http://example.com/file.txt`

What it does:
Resumes a download from where it left off (useful for large files).

Command-line Arguments Explained:

- `-C -`: Tells curl to resume the download from the last position.

---

## Verify SSL certificate

Example Usage:
`curl --cacert /path/to/ca-cert.pem https://example.com`

What it does:
Verifies the SSL certificate against a specified CA certificate file.

Command-line Arguments Explained:

- `--cacert /path/to/ca-cert.pem`: Specifies the path to a CA certificate file for validation.

---

## Send HTTP/1.1 request

Example Usage:
`curl -1 http://example.com`

What it does:
Forces curl to use HTTP/1.1 instead of the default protocol (usually HTTP/1.1 or HTTP/2).

Command-line Arguments Explained:

- `-1`: Uses HTTP/1.1 protocol.

---

## Send DELETE request

Example Usage:
`curl -X DELETE http://example.com/api/resource/123`

What it does:
Sends a DELETE request to remove a resource at the specified URL.

Command-line Arguments Explained:

- `-X DELETE`: Specifies the HTTP request method as DELETE.
