# cURL — Transfer Data from the Command Line

> Practical guide to cURL — HTTP requests, headers, authentication, downloads and TLS on the command line, with examples for every use case.

Source: https://www.jpkc.com/db/en/cheatsheets/networking/curl/

<!-- PROSE:intro -->
cURL is the Swiss Army knife for HTTP on the command line: a single command lets you call APIs, download files, probe endpoints or debug headers. It speaks more than twenty protocols – from HTTP and HTTPS through FTP to SFTP – and ships on virtually every system. This guide walks you through the options you reach for daily, from a plain GET request to client-certificate authentication.
<!-- PROSE:intro:end -->

## Basic Requests

`curl <url>` — Perform a simple GET request and print the response to stdout.

```bash
curl https://api.example.com/users
```

`curl -s <url>` — Silent mode. Suppress progress meter and error messages.

```bash
curl -s https://api.example.com/users
```

`curl -v <url>` — Verbose mode. Show request and response headers for debugging.

```bash
curl -v https://api.example.com/users
```

`curl -I <url>` — Fetch only the HTTP response headers (HEAD request).

```bash
curl -I https://www.example.com
```

`curl -L <url>` — Follow redirects automatically.

```bash
curl -L https://example.com/redirect
```

## Headers

`curl -H "<header>" <url>` — Send a custom request header.

```bash
curl -H "Accept: application/json" https://api.example.com/users
```

`curl -H "Content-Type: application/json" <url>` — Set the content type header for the request.

```bash
curl -H "Content-Type: application/json" https://api.example.com/users
```

`curl -H "Authorization: Bearer <token>" <url>` — Send a Bearer token for authentication.

```bash
curl -H "Authorization: Bearer eyJhbGci..." https://api.example.com/me
```

`curl -A "<user-agent>" <url>` — Set a custom User-Agent string.

```bash
curl -A "Mozilla/5.0" https://www.example.com
```

## POST & PUT Data

`curl -X POST -d "<data>" <url>` — Send a POST request with URL-encoded form data.

```bash
curl -X POST -d "name=John&email=john@example.com" https://api.example.com/users
```

`curl -X POST -H "Content-Type: application/json" -d '<json>' <url>` — Send a POST request with JSON body.

```bash
curl -X POST -H "Content-Type: application/json" -d '{"name":"John"}' https://api.example.com/users
```

`curl -X PUT -d '<data>' <url>` — Send a PUT request to update a resource.

```bash
curl -X PUT -H "Content-Type: application/json" -d '{"name":"Jane"}' https://api.example.com/users/1
```

`curl -X PATCH -d '<data>' <url>` — Send a PATCH request for partial updates.

```bash
curl -X PATCH -H "Content-Type: application/json" -d '{"email":"new@example.com"}' https://api.example.com/users/1
```

`curl -X DELETE <url>` — Send a DELETE request to remove a resource.

```bash
curl -X DELETE https://api.example.com/users/1
```

`curl -F "file=@<path>" <url>` — Upload a file using multipart form data.

```bash
curl -F "file=@photo.jpg" https://api.example.com/upload
```

`curl -d @<file> <url>` — Send data from a file as the request body.

```bash
curl -d @data.json -H "Content-Type: application/json" https://api.example.com/users
```

## Authentication

`curl -u <user>:<password> <url>` — Send a request with Basic authentication.

```bash
curl -u admin:secret https://api.example.com/admin
```

`curl -u <user> <url>` — Prompt for password interactively (more secure).

```bash
curl -u admin https://api.example.com/admin
```

`curl --oauth2-bearer <token> <url>` — Send a request with an OAuth 2.0 Bearer token.

```bash
curl --oauth2-bearer eyJhbGci... https://api.example.com/me
```

`curl -b "<cookies>" <url>` — Send cookies with the request.

```bash
curl -b "session=abc123" https://www.example.com/dashboard
```

`curl -c <file> <url>` — Save response cookies to a file.

```bash
curl -c cookies.txt https://www.example.com/login
```

## Download & Output

`curl -o <file> <url>` — Save the response to a specific file.

```bash
curl -o page.html https://www.example.com
```

`curl -O <url>` — Save the response using the remote filename.

```bash
curl -O https://example.com/file.tar.gz
```

`curl -w "%{http_code}" -o /dev/null -s <url>` — Get only the HTTP status code.

```bash
curl -w "%{http_code}" -o /dev/null -s https://www.example.com
```

`curl -w "%{time_total}" -o /dev/null -s <url>` — Measure the total response time.

```bash
curl -w "%{time_total}" -o /dev/null -s https://www.example.com
```

`curl -C - -O <url>` — Resume an interrupted download.

```bash
curl -C - -O https://example.com/largefile.zip
```

## SSL/TLS

`curl -k <url>` — Skip SSL certificate verification (insecure, for testing only).

```bash
curl -k https://self-signed.example.com
```

`curl --cacert <file> <url>` — Use a custom CA certificate for verification.

```bash
curl --cacert ca-bundle.crt https://api.example.com
```

`curl --cert <cert> --key <key> <url>` — Use client certificate authentication.

```bash
curl --cert client.crt --key client.key https://api.example.com
```

## Advanced Options

`curl -x <proxy> <url>` — Use a proxy server for the request.

```bash
curl -x http://proxy.example.com:8080 https://www.example.com
```

`curl --connect-timeout <seconds> <url>` — Set maximum time for the connection phase.

```bash
curl --connect-timeout 5 https://api.example.com
```

`curl -m <seconds> <url>` — Set maximum time for the entire operation.

```bash
curl -m 30 https://api.example.com/slow-endpoint
```

`curl --retry <num> <url>` — Retry the request on transient errors.

```bash
curl --retry 3 https://api.example.com/flaky
```

`curl -s <url> | jq .` — Pipe JSON response to jq for pretty-printing.

```bash
curl -s https://api.example.com/users | jq .
```

<!-- PROSE:outro -->
## Conclusion

cURL belongs in every developer's and administrator's toolkit: what starts as a quick API test soon replaces whole scripts. If you regularly build complex requests, it pays to explore `--config` files and the `-w` output format for machine-readable results – turning a one-liner into a reusable tool.

## Further Reading

- [curl – official documentation](https://curl.se/docs/) – reference and manual
- [curl(1) – manual page](https://curl.se/docs/manpage.html) – every option at a glance
- [cURL – Wikipedia](https://en.wikipedia.org/wiki/CURL) – background and history
<!-- PROSE:outro:end -->

## Related Commands

- [httpie](https://www.jpkc.com/db/en/cheatsheets/networking/httpie/) – modern, user-friendly HTTP client
- [wget](https://www.jpkc.com/db/en/cheatsheets/networking/wget/) – download files and entire sites
- [dig](https://www.jpkc.com/db/en/cheatsheets/networking/dig/) – check DNS resolution before sending requests

