cURL 命令速查表
cURL 是命令行下的网络请求工具,几乎所有的 Linux/macOS 都内置了 curl。本速查表覆盖了日常开发中最常用的 curl 用法,从基本的 GET 请求到高级的代理配置和调试技巧,每个命令都有实际使用示例。无论你是调试 API 还是批量下载文件,这份速查表都能帮到你。
使用场景速查
| 你要做什么 | 用哪个命令 |
| 发送 GET 请求 | curl https://api.example.com |
| 发送 POST 请求 | curl -X POST -d 'key=val' URL |
| 设置请求头 | curl -H "Authorization: Bearer TOKEN" URL |
| 下载文件 | curl -O url 或 curl -o output url |
| 调试请求过程 | curl -v URL |
| 发送 JSON 数据 | curl -X POST -H 'Content-Type: application/json' -d '{"key":"val"}' URL |
| 上传文件 | curl -F "file=@photo.jpg" URL |
| 使用代理 | curl -x http://proxy:8080 URL |
请求方法
``bash
# GET 请求(默认)
curl https://api.github.com/users/octocat
# POST 请求 curl -X POST https://httpbin.org/post \ -d "username=alice&password=secret"
# POST 发送 JSON curl -X POST https://httpbin.org/post \ -H "Content-Type: application/json" \ -d '{"name":"Alice","email":"alice@example.com"}'
# PUT 请求(更新资源) curl -X PUT https://api.example.com/users/1 \ -H "Content-Type: application/json" \ -d '{"name":"Alice Updated"}'
# DELETE 请求 curl -X DELETE https://api.example.com/users/1
# PATCH 请求(部分更新) curl -X PATCH https://api.example.com/users/1 \ -H "Content-Type: application/json" \ -d '{"email":"new@example.com"}'
# HEAD 请求(仅查看响应头) curl -I https://example.com
# OPTIONS 请求(查看支持的请求方法)
curl -X OPTIONS https://api.example.com -i
`
请求头与响应头
`bash
# 设置请求头
curl -H "Accept: application/json" https://api.example.com
# 多个请求头 curl -H "Accept: application/json" \ -H "Authorization: Bearer token123" \ -H "X-Custom-Header: custom-value" \ https://api.example.com
# 设置 User-Agent curl -A "Mozilla/5.0 (Linux; Android 13) Chrome/120" \ https://example.com
# 设置 Referer curl -e "https://google.com" https://example.com
# 查看响应头(-i 包含响应头在输出中) curl -i https://example.com
# 仅查看响应头(-I 发送 HEAD 请求) curl -I https://example.com
# 自定义 Cookie 请求头 curl -H "Cookie: sessionId=abc123; theme=dark" \ https://example.com
# 限制响应头返回(-r 范围请求)
curl -r 0-1023 https://example.com/file.zip
`
认证与鉴权
`bash
# Basic Auth(用户名密码)
curl -u "alice:password123" https://api.example.com/secure
# Basic Auth(只提示用户名,交互式输入密码,更安全) curl -u "alice" https://api.example.com/secure
# Bearer Token(JWT / OAuth2) curl -H "Authorization: Bearer eyJhbGciOiJIUzI1NiJ9..." \ https://api.example.com/protected
# Digest Authentication curl --digest -u "alice:password" \ https://api.example.com/digest-auth
# .netrc 文件认证(免输入密码) # 在 ~/.netrc 中写入: # machine api.example.com login alice password secret curl -n https://api.example.com/secure
# Client Certificate(mTLS) curl --cert client.pem --key client-key.pem \ https://api.example.com/mtls
# OAuth2 Client Credentials 获取 Token curl -X POST https://auth.example.com/oauth/token \ -d "grant_type=client_credentials" \ -d "client_id=myapp" \ -d "client_secret=mysecret"
# API Key(查询参数方式)
curl "https://api.example.com/data?api_key=sk-xxxxx"
`
文件上传
`bash
# 表单上传文件(multipart/form-data)
curl -F "file=@report.pdf" \
-F "title=年度报告" \
https://api.example.com/upload
# 上传文件并指定 MIME 类型 curl -F "image=@photo.jpg;type=image/jpeg" \ https://api.example.com/upload
# PUT 上传文件(直接二进制内容) curl -T report.pdf \ -H "Content-Type: application/pdf" \ https://api.example.com/documents/report.pdf
# 下载文件(保留原文件名) curl -O https://example.com/file.zip
# 下载文件(指定输出文件名) curl -o output.txt https://example.com/data.json
# 断点续传下载 curl -C - -O https://example.com/large-file.iso
# 限制下载速度(速率限制) curl --limit-rate 1M -O https://example.com/big-file.zip
# FTP 上传 curl -T file.txt ftp://ftp.example.com/upload/ \ --user "alice:password"
# FTP 下载 curl ftp://ftp.example.com/file.txt --user "alice:password"
# 大文件下载(跟随重定向)
curl -L -O https://example.com/latest-release.tar.gz
`
调试与诊断
`bash
# 详细模式(查看请求和响应头)
curl -v https://api.example.com
# 查看完整请求过程(包括 SSL 握手) curl --trace-ascii trace.log https://api.example.com
# 二进制跟踪(最详细的调试方式) curl --trace trace.bin https://api.example.com
# 仅查看耗时统计 curl -w "\n时间统计:\n\ time_namelookup: %{time_namelookup}s\n\ time_connect: %{time_connect}s\n\ time_appconnect: %{time_appconnect}s\n\ time_pretransfer: %{time_pretransfer}s\n\ time_redirect: %{time_redirect}s\n\ time_starttransfer: %{time_starttransfer}s\n\ time_total: %{time_total}s\n\ 下载大小: %{size_download} 字节\n\ 响应码: %{http_code}\n" \ -o /dev/null -s https://api.example.com
# 连接超时设置 curl --connect-timeout 10 --max-time 30 \ https://example.com
# 静默模式(不显示进度条和错误) curl -s https://api.example.com
# 静默但显示错误 curl -sS https://api.example.com
# 显示响应头(不包含响应体) curl -s -o /dev/null -w "%{http_code}" \ https://api.example.com
# 跟随重定向(最多 5 次) curl -L --max-redirs 5 https://httpbin.org/redirect/3
# 跳过 SSL 证书验证(仅测试环境) curl -k https://self-signed.example.com
# 指定 CA 证书文件 curl --cacert /path/to/ca-bundle.crt \ https://api.example.com
# 指定 SSL 版本 curl --tlsv1.2 https://secure.example.com
# 设置 DNS 解析(绕过 DNS) curl --resolve "example.com:443:127.0.0.1" \ https://example.com
# 忽略响应体,只返回 HTTP 状态码
curl -s -o /dev/null -w "%{http_code}" \
https://api.example.com/health
`
Cookie 管理
`bash
# 发送 Cookie(请求头方式)
curl -H "Cookie: session=abc123; user=alice" \
https://example.com
# 保存服务器 Set-Cookie 到文件(cookie jar) curl -c cookies.txt https://example.com/login
# 从文件读取 Cookie 并发送 curl -b cookies.txt https://example.com/dashboard
# 同时保存和发送 Cookie(会话保持) curl -c cookies.txt -b cookies.txt \ https://example.com/login
# 先登录获取 Cookie,再用 Cookie 访问 curl -c session.txt -X POST \ -d "username=alice&password=secret" \ https://api.example.com/auth/login
curl -b session.txt \ https://api.example.com/user/profile
# 忽略 Cookie(每次请求都不保存 Cookie) curl --no-cookie https://example.com
# 空 Cookie(清空所有 Cookie 发送空 Cookie 头)
curl -b "NONE" https://example.com
`
代理配置
`bash
# HTTP 代理
curl -x http://proxy.example.com:8080 \
https://api.example.com
# HTTPS 代理 curl -x https://proxy.example.com:443 \ https://api.example.com
# SOCKS5 代理 curl --socks5 127.0.0.1:1080 \ https://api.example.com
# SOCKS5 主机名解析走代理 curl --socks5-hostname 127.0.0.1:1080 \ https://api.example.com
# SOCKS4 代理 curl --socks4 127.0.0.1:1080 \ https://api.example.com
# 带认证的代理 curl -x http://proxy.example.com:8080 \ --proxy-user "user:password" \ https://api.example.com
# 跳过代理(直连) curl --noproxy "localhost,127.0.0.1,*.local" \ https://api.example.com
# 从环境变量读取代理 # export http_proxy=http://proxy:8080 # export https_proxy=http://proxy:8080 curl https://api.example.com
# 忽略环境变量中的代理 curl --noproxy "*" https://api.example.com ``
请求方法(7)
| 命令 | 难度 | ||
|---|---|---|---|
curl <url>发送 GET 请求(默认请求方法) | 基础 | curl https://api.github.com/users/octocat | |
curl -X POST发送 POST 请求 | 基础 | curl -X POST https://httpbin.org/post -d "key=val" | |
curl -X PUT发送 PUT 请求(更新资源) | 中级 | curl -X PUT https://api.example.com/users/1 -d "{\"name\":\"Alice\"}" | |
curl -X DELETE发送 DELETE 请求 | 中级 | curl -X DELETE https://api.example.com/users/1 | |
curl -X PATCH发送 PATCH 请求(部分更新) | 中级 | curl -X PATCH https://api.example.com/users/1 -d "{\"email\":\"new@example.com\"}" | |
curl -I发送 HEAD 请求(仅查看响应头) | 基础 | curl -I https://example.com | |
curl -X OPTIONS发送 OPTIONS 请求(查看支持的请求方法) | 中级 | curl -X OPTIONS https://api.example.com -i |
请求头与响应头(4)
| 命令 | 难度 | ||
|---|---|---|---|
curl -H设置自定义请求头 | 基础 | curl -H "Accept: application/json" https://api.example.com | |
curl -A设置 User-Agent 请求头 | 基础 | curl -A "Mozilla/5.0 (Linux; Android 13)" https://example.com | |
curl -e设置 Referer 请求头 | 中级 | curl -e "https://google.com" https://example.com | |
curl -i在输出中包含响应头 | 基础 | curl -i https://example.com |
认证与鉴权(4)
| 命令 | 难度 | ||
|---|---|---|---|
curl -uHTTP Basic 认证 | 基础 | curl -u "alice:password123" https://api.example.com/secure | |
curl -H "Authorization: Bearer <token>"Bearer Token 认证(JWT / OAuth2) | 中级 | curl -H "Authorization: Bearer eyJhbGci..." https://api.example.com/protected | |
curl --digestHTTP Digest 摘要认证 | 中级 | curl --digest -u "alice:password" https://api.example.com/digest-auth | |
curl -n使用 .netrc 文件进行认证 | 中级 | curl -n https://api.example.com/secure |
文件上传(8)
| 命令 | 难度 | ||
|---|---|---|---|
curl -F表单文件上传(multipart/form-data) | 中级 | curl -F "file=@report.pdf" -F "title=Report" https://api.example.com/upload | |
curl -TPUT 方式上传文件(直接二进制内容) | 中级 | curl -T report.pdf -H "Content-Type: application/pdf" https://api.example.com/doc.pdf | |
curl -O下载文件并保留原文件名 | 基础 | curl -O https://example.com/file.zip | |
curl -o下载文件并指定输出文件名 | 基础 | curl -o output.txt https://example.com/data.json | |
curl -C -断点续传下载 | 中级 | curl -C - -O https://example.com/large-file.iso | |
curl --limit-rate限制下载/上传速度 | 中级 | curl --limit-rate 1M -O https://example.com/big-file.zip | |
curl -T ftp://FTP 上传文件 | 中级 | curl -T file.txt ftp://ftp.example.com/ --user "alice:password" | |
curl -L -O跟随重定向下载文件 | 基础 | curl -L -O https://example.com/latest-release.tar.gz |
调试与诊断(8)
| 命令 | 难度 | ||
|---|---|---|---|
curl -v详细模式 — 显示请求和响应头 | 基础 | curl -v https://api.example.com | |
curl --trace-ascii跟踪请求过程(含 SSL 握手) | 中级 | curl --trace-ascii trace.log https://api.example.com | |
curl -w自定义输出格式(显示耗时统计等) | 中级 | curl -w "%{http_code} %{time_total}s" -o /dev/null -s https://example.com | |
curl --connect-timeout --max-time设置连接超时和最大请求时间 | 中级 | curl --connect-timeout 10 --max-time 30 https://example.com | |
curl -s / -sS静默模式(隐藏进度条) | 基础 | curl -sS https://api.example.com | |
curl -k跳过 SSL 证书验证(仅测试环境) | 中级 | curl -k https://self-signed.example.com | |
curl -L --max-redirs跟随重定向并限制重定向次数 | 中级 | curl -L --max-redirs 5 https://httpbin.org/redirect/3 | |
curl --resolve自定义 DNS 解析(指定 IP) | 高级 | curl --resolve "example.com:443:127.0.0.1" https://example.com |
Cookie 管理(4)
| 命令 | 难度 | ||
|---|---|---|---|
curl -c <file>保存 Set-Cookie 到文件(cookie jar) | 中级 | curl -c cookies.txt https://example.com/login | |
curl -b <file>从文件读取 Cookie 并发送 | 中级 | curl -b cookies.txt https://example.com/dashboard | |
curl -c <file> -b <file>Cookie 会话保持(同时读写) | 中级 | curl -c session.txt -b session.txt -d "user=alice&pass=secret" https://api.example.com/login | |
curl --no-cookie忽略 Cookie(不保存也不发送) | 中级 | curl --no-cookie https://example.com |
代理配置(5)
| 命令 | 难度 | ||
|---|---|---|---|
curl -x <proxy>使用 HTTP/HTTPS 代理 | 中级 | curl -x http://proxy.example.com:8080 https://api.example.com | |
curl --socks5使用 SOCKS5 代理 | 中级 | curl --socks5 127.0.0.1:1080 https://api.example.com | |
curl --socks5-hostname使用 SOCKS5 代理(DNS 解析走代理) | 中级 | curl --socks5-hostname 127.0.0.1:1080 https://api.example.com | |
curl --proxy-user带认证的代理 | 中级 | curl -x http://proxy:8080 --proxy-user "user:pass" https://api.example.com | |
curl --noproxy跳过代理(直连指定域名) | 中级 | curl --noproxy "localhost,127.0.0.1,*.local" https://api.example.com |