|
| 1 | +curl (transfer data to/from servers) |
| 2 | + |
| 3 | +Description: |
| 4 | + curl is a command-line tool for transferring data over network protocols. |
| 5 | + In practice, it is most often used for HTTP/HTTPS: downloading files, |
| 6 | + calling APIs, and inspecting requests and responses. |
| 7 | + |
| 8 | +Usage: |
| 9 | + curl [OPTIONS] URL |
| 10 | + |
| 11 | + curl has many options. This sheet focuses on common, practical ones. |
| 12 | + |
| 13 | +Output / downloads: |
| 14 | + -o, --output <file> |
| 15 | + Save the response body to a specific file name. |
| 16 | + Example: curl -o page.html https://example.com |
| 17 | + |
| 18 | + -O, --remote-name |
| 19 | + Save using the remote file name (derived from the URL path). |
| 20 | + Example: curl -O https://example.com/file.zip |
| 21 | + |
| 22 | + -L, --location |
| 23 | + Follow HTTP redirects. |
| 24 | + Example: curl -L https://example.com |
| 25 | + |
| 26 | +HTTP inspection: |
| 27 | + -I, --head |
| 28 | + Fetch headers only (send an HTTP HEAD request). |
| 29 | + Example: curl -I https://example.com |
| 30 | + |
| 31 | + -i, --include |
| 32 | + Include response headers in the output. |
| 33 | + |
| 34 | +Requests (methods, headers, bodies): |
| 35 | + -X, --request <method> |
| 36 | + Set the HTTP method (GET, POST, PUT, PATCH, DELETE, ...). |
| 37 | + Example: curl -X DELETE https://api.example.com/item/123 |
| 38 | + |
| 39 | + -H, --header <header> |
| 40 | + Add a request header. |
| 41 | + Example: curl -H "Authorization: Bearer <token>" https://api.example.com/me |
| 42 | + |
| 43 | + -d, --data <data> |
| 44 | + Send request body data. Commonly used for form-style payloads. |
| 45 | + (Often implies POST if -X is not specified.) |
| 46 | + Example: curl -d "name=alice&role=admin" https://api.example.com/users |
| 47 | + |
| 48 | + --data-urlencode <data> |
| 49 | + Like --data, but URL-encodes the value; useful for special characters. |
| 50 | + Example: curl --data-urlencode "q=a+b" https://api.example.com/search |
| 51 | + |
| 52 | + -F, --form <name=content> |
| 53 | + Submit multipart/form-data (typical for file uploads). |
| 54 | + Example: curl -F "file=@report.pdf" https://api.example.com/upload |
| 55 | + |
| 56 | +Authentication: |
| 57 | + -u, --user <user:password> |
| 58 | + Use HTTP Basic authentication. |
| 59 | + Example: curl -u user:pass https://example.com/private |
| 60 | + |
| 61 | +Console output and failure behavior: |
| 62 | + -s, --silent |
| 63 | + Suppress progress meter and most messages. |
| 64 | + |
| 65 | + -S, --show-error |
| 66 | + When used with -s, still show errors. |
| 67 | + Example: curl -sS https://example.com |
| 68 | + |
| 69 | + -f, --fail |
| 70 | + Treat HTTP errors as failures (non-zero exit; body may be suppressed). |
| 71 | + |
| 72 | +Debugging: |
| 73 | + -v, --verbose |
| 74 | + Show request/response details (useful for HTTP/TLS troubleshooting). |
| 75 | + |
| 76 | +Timeouts / retries: |
| 77 | + --retry <num> |
| 78 | + Retry up to <num> times on transient failures. |
| 79 | + |
| 80 | + --connect-timeout <seconds> |
| 81 | + Maximum time allowed for the connection phase. |
| 82 | + |
| 83 | + -m, --max-time <seconds> |
| 84 | + Maximum time allowed for the entire transfer. |
| 85 | + |
| 86 | +TLS/HTTPS (use carefully): |
| 87 | + -k, --insecure |
| 88 | + Skip TLS certificate verification. Avoid in production. |
| 89 | + |
| 90 | + --cacert <file> |
| 91 | + Use a custom CA bundle. |
| 92 | + |
| 93 | +Help/version: |
| 94 | + --help |
| 95 | + Show curl help. |
| 96 | + |
| 97 | + --version |
| 98 | + Show curl version and supported features. |
| 99 | + |
| 100 | +Examples: |
| 101 | + curl -sS -L https://example.com |
| 102 | + Quiet download with errors shown; follows redirects. |
| 103 | + |
| 104 | + curl -X POST -H "Content-Type: application/json" -d '{"name":"Alice"}' https://api.example.com/users |
| 105 | + POST JSON to an API endpoint. |
0 commit comments