WebsiteTemplate/cheatsheets/curl.html
2026-01-25 11:33:37 -04:00

307 lines
9.1 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self'; font-src 'self' data:; img-src 'self' data:; connect-src 'self'; base-uri 'self'; form-action 'self' https://defcon.social https://bsky.app;">
<meta http-equiv="X-Content-Type-Options" content="nosniff">
<link rel="stylesheet" href="../assets/css/style.css">
<link rel="icon" type="image/x-icon" href="../favicon.ico">
<script>
// Apply theme immediately to prevent flash
(function() {
const theme = localStorage.getItem('theme') ||
(window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light');
document.documentElement.setAttribute('data-theme', theme);
})();
</script>
<title>curl Cheatsheet - Cheatsheets - Launch Pad</title>
</head>
<body>
<button class="theme-toggle" id="themeToggle" aria-label="Toggle dark mode">
<svg class="theme-icon theme-icon-moon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path></svg>
<svg class="theme-icon theme-icon-sun" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="display: none;"><circle cx="12" cy="12" r="5"></circle><line x1="12" y1="1" x2="12" y2="3"></line><line x1="12" y1="21" x2="12" y2="23"></line><line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line><line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line><line x1="1" y1="12" x2="3" y2="12"></line><line x1="21" y1="12" x2="23" y2="12"></line><line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line><line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line></svg>
</button>
<br/><br/>
<div class="name">
__ _______________________ _________._________________________
\_ _____/ \______ \ / _ \ / _____/ / _____/ | | \_ _____/
| __) | _/ / /_\ \ / \ ___ / \ ___ | | | __)_
| \ | | \ / | \ \ \_\ \ \ \_\ \ | |___ | \
\___ / |____|_ / \____|__ / \______ / \______ / |_______ \ /_______ /
\/ \/ \/ \/ \/ \/ \/
</div>
<div class="blog-page-header">
<div class="blog-header-content">
<a href="/cheatsheets" class="back-link" title="Back to Cheatsheets">
<svg xmlns="http://www.w3.org/2000/svg" width="42" height="42" viewBox="0 0 24 24" class="home-icon"><path fill="currentColor" d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z"/></svg>
</a>
<h1 class="blog-page-title">curl Cheatsheet</h1>
</div>
</div>
<div class="blog-post-container">
<div class="blog-posts-container" style="max-width: 900px; margin: 0 auto;">
<div class="blog-post">
<div class="blog-post-content">
<p><a href="index.html">← Back to cheatsheets</a></p>
<p><a href="../index.html">← Home</a></p>
<hr>
<p>curl is a command-line tool for transferring data with URLs. It supports HTTP, HTTPS, FTP, and many other protocols. Essential for API testing, web scraping, and general HTTP operations.</p>
<hr>
<h2>Basic Usage</h2>
<ul>
<li>curl &lt;url&gt; - GET request, output to stdout</li>
</ul>
<ul>
<li>curl -o file.html &lt;url&gt; - Save to file</li>
</ul>
<ul>
<li>curl -O &lt;url&gt; - Save with remote filename</li>
</ul>
<hr>
<h2>HTTP Methods</h2>
<ul>
<li>curl -X GET &lt;url&gt; - GET request (default)</li>
</ul>
<ul>
<li>curl -X POST &lt;url&gt; - POST request</li>
</ul>
<ul>
<li>curl -X PUT &lt;url&gt; - PUT request</li>
</ul>
<ul>
<li>curl -X DELETE &lt;url&gt; - DELETE request</li>
</ul>
<ul>
<li>curl -X PATCH &lt;url&gt; - PATCH request</li>
</ul>
<ul>
<li>curl -I &lt;url&gt; - HEAD request (headers only)</li>
</ul>
<hr>
<h2>Headers</h2>
<ul>
<li>-H "Header: Value" - Add custom header</li>
</ul>
<ul>
<li>-H "Content-Type: application/json" - Set content type</li>
</ul>
<ul>
<li>-H "Authorization: Bearer token" - Auth header</li>
</ul>
<ul>
<li>-A "User-Agent" - Set User-Agent</li>
</ul>
<ul>
<li>-e "Referer" - Set Referer header</li>
</ul>
<hr>
<h2>Data/Body</h2>
<ul>
<li>-d "data" - Send POST data</li>
</ul>
<ul>
<li>-d @file.json - POST data from file</li>
</ul>
<ul>
<li>--data-urlencode "param=value" - URL encode data</li>
</ul>
<ul>
<li>-F "file=@path/file.txt" - Form upload</li>
</ul>
<ul>
<li>-F "field=value" - Form field</li>
</ul>
<hr>
<h2>Output Options</h2>
<ul>
<li>-o filename - Output to file</li>
</ul>
<ul>
<li>-O - Save with remote filename</li>
</ul>
<ul>
<li>-s - Silent mode (no progress)</li>
</ul>
<ul>
<li>-S - Show errors in silent mode</li>
</ul>
<ul>
<li>-v - Verbose output</li>
</ul>
<ul>
<li>-i - Include response headers</li>
</ul>
<ul>
<li>-w "%{http_code}" - Write out format</li>
</ul>
<hr>
<h2>Authentication</h2>
<ul>
<li>-u user:password - Basic auth</li>
</ul>
<ul>
<li>--digest - Digest auth</li>
</ul>
<ul>
<li>--ntlm - NTLM auth</li>
</ul>
<ul>
<li>--negotiate - Negotiate auth</li>
</ul>
<ul>
<li>-E cert.pem - Client certificate</li>
</ul>
<hr>
<h2>Cookies</h2>
<ul>
<li>-b "name=value" - Send cookie</li>
</ul>
<ul>
<li>-b cookies.txt - Read cookies from file</li>
</ul>
<ul>
<li>-c cookies.txt - Save cookies to file</li>
</ul>
<ul>
<li>-c - - Print cookies to stdout</li>
</ul>
<hr>
<h2>SSL/TLS</h2>
<ul>
<li>-k - Ignore SSL errors</li>
</ul>
<ul>
<li>--cacert file - CA certificate</li>
</ul>
<ul>
<li>--cert file - Client certificate</li>
</ul>
<ul>
<li>--key file - Private key</li>
</ul>
<ul>
<li>--tlsv1.2 - Force TLS 1.2</li>
</ul>
<ul>
<li>--tlsv1.3 - Force TLS 1.3</li>
</ul>
<hr>
<h2>Proxy</h2>
<ul>
<li>-x proxy:port - HTTP proxy</li>
</ul>
<ul>
<li>-x socks5://proxy:port - SOCKS5 proxy</li>
</ul>
<ul>
<li>--proxy-user user:pass - Proxy auth</li>
</ul>
<ul>
<li>--noproxy "localhost" - Bypass proxy</li>
</ul>
<hr>
<h2>Redirects & Timeouts</h2>
<ul>
<li>-L - Follow redirects</li>
</ul>
<ul>
<li>--max-redirs N - Max redirects</li>
</ul>
<ul>
<li>--connect-timeout N - Connection timeout</li>
</ul>
<ul>
<li>-m N - Max time for operation</li>
</ul>
<ul>
<li>--retry N - Retry count</li>
</ul>
<hr>
<h2>Common Examples</h2>
<h3>Simple GET</h3>
<pre><code>curl https://api.example.com/data</code></pre>
<p>Basic GET request.</p>
<h3>POST JSON</h3>
<pre><code>curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' https://api.example.com/data</code></pre>
<p>POST JSON data.</p>
<h3>Download File</h3>
<pre><code>curl -O https://example.com/file.zip</code></pre>
<p>Download and save with original name.</p>
<h3>With Auth</h3>
<pre><code>curl -u admin:password https://api.example.com/admin</code></pre>
<p>Basic authentication.</p>
<h3>Headers Only</h3>
<pre><code>curl -I https://example.com</code></pre>
<p>Get response headers only.</p>
<h3>Verbose Debug</h3>
<pre><code>curl -v https://example.com</code></pre>
<p>See full request/response details.</p>
<h3>Follow Redirects</h3>
<pre><code>curl -L https://example.com/redirect</code></pre>
<p>Follow HTTP redirects.</p>
<h3>Upload File</h3>
<pre><code>curl -F "file=@/path/to/file.txt" https://example.com/upload</code></pre>
<p>Upload file via form.</p>
<h3>Status Code Only</h3>
<pre><code>curl -s -o /dev/null -w "%{http_code}" https://example.com</code></pre>
<p>Get only HTTP status code.</p>
<h3>Through Proxy</h3>
<pre><code>curl -x http://proxy:8080 https://example.com</code></pre>
<p>Route through HTTP proxy.</p>
<h3>Save Cookies</h3>
<pre><code>curl -c cookies.txt -b cookies.txt https://example.com/login</code></pre>
<p>Manage session cookies.</p>
<h3>API Token</h3>
<pre><code>curl -H "Authorization: Bearer eyJhbGc..." https://api.example.com/user</code></pre>
<p>Request with bearer token.</p>
<hr>
<h2>Write-Out Variables</h2>
<p>Use with -w option:</p>
<ul>
<li>%{http_code} - HTTP status code</li>
<li>%{time_total} - Total time</li>
<li>%{time_connect} - Time to connect</li>
<li>%{size_download} - Downloaded bytes</li>
<li>%{speed_download} - Download speed</li>
<li>%{url_effective} - Final URL</li>
<li>%{redirect_url} - Redirect URL</li>
</ul>
<hr>
<h2>Tips</h2>
<ul>
<li>Use -s for scripts (silent mode)</li>
</ul>
<ul>
<li>Use -v to debug connection issues</li>
</ul>
<ul>
<li>Use -L to handle redirects automatically</li>
</ul>
<ul>
<li>Use -k only for testing (insecure)</li>
</ul>
<ul>
<li>Combine -sS for silent but show errors</li>
</ul>
<ul>
<li>Use @filename to read data from files</li>
</ul>
<ul>
<li>Essential for API testing and debugging</li>
</ul>
<ul>
<li>Supports many protocols beyond HTTP</li>
</ul>
<hr>
<p><a href="index.html">← Back to cheatsheets</a></p>
<p><a href="../index.html">← Home</a></p>
</div>
</div>
</div>
</div>
<script async type="text/javascript" src="../blog/analytics.js"></script>
<script src="../theme.js"></script>
</body>
</html>