Curl 命令行请求工具

精选 Curl 命令行请求工具 常用指令与核心速查备忘单,涵盖高频用法、配置参数与实用技巧。

#⚙️ 选项参数

-o <file>    # --output: 保存输出到本地文件
-u user:pass # --user: HTTP 身份认证账号密码

-v   # --verbose: 详细输出模式 (显示请求头和响应头)
-vv  # 更加详细的调试日志
-s   # --silent: 静默模式,不显示进度条和错误
-S   # --show-error: 与 --silent (-sS) 配合使用,隐藏进度条但显示错误

-i  # --include: 在输出中包含 HTTP 响应头
-I  # --head: 仅获取 HTTP 响应头 (HEAD 请求)

#请求控制 (Request)

-X POST # --request: 指定 HTTP 方法
-L      # --location: 如果页面发生 301/302 重定向,自动追踪重定向链接
-F      # --form: 模拟表单 multipart/form-data 数据提交

#数据传输 (Data)

# --data: HTTP POST 报文数据
# URL 编码形式 (如 status="Hello")
-d 'data'

# --data 从文件中读取数据内容
-d @file

# --get: 将 -d 的数据通过 GET 查询参数追加发送
-G
-A <str>      # --user-agent: 设置客户端 User-Agent 字符串

-b name=val   # --cookie: 传递单个 Cookie 键值对

-b, --cookie FILE           # 从指定的 Cookie 文件中读取 Cookies
-c, --cookie-jar FILE       # 将接收到的 Cookies 保存到指定文件

-H "X-Foo: y" # --header: 添加自定义 HTTP 请求头

--compressed  # 请求使用 deflate/gzip 压缩响应

#SSL 安全证书 (SSL)

    --cacert <file> # 指定 CA 证书文件
    --capath <dir>  # 指定 CA 证书目录路径
-E, --cert <cert> # --cert: 客户端证书文件
    --cert-type # 证书格式类型 (der/pem/eng)
-k, --insecure # 允许不安全的 SSL 连接 (忽略自签名证书错误)

安装依赖

apk add --update curl # 在 Alpine Linux 中安装 curl

#常用实战示例 (Example)

#CURL GET / HEAD 请求

命令指令 含义说明
curl -I https://qr.warpnav.com 仅发送 HEAD 请求获取响应头
curl -v -I https://qr.warpnav.com 带详细过程日志的 HEAD 请求
curl -X GET https://qr.warpnav.com 使用显式定义的 GET HTTP 方法
curl --noproxy 127.0.0.1 http://www.stackoverflow.com 不使用 HTTP 代理发起请求
curl --connect-timeout 10 -I -k https://qr.warpnav.com 设置 10 秒连接超时且忽略自签名证书
curl --verbose --header "Host: www.mytest.com:8182" qr.warpnav.com 带自定义 Host 请求头的高级请求
curl -k -v https://www.google.com 获取带响应头的详细调试数据

#多文件上传 (Multiple file upload)

$ curl -v --include \
--form key1=value1 \
    --form upload=@localfilename URL

#格式化美化 JSON 响应体

$ curl -XGET http://${elasticsearch_ip}:9200/_cluster/nodes | python -m json.tool

#CURL POST 请求示例

命令指令 含义说明
curl -d "name=username&password=123456" <URL> 发送表单序列化 POST 数据
curl <URL> -H "content-type: application/json" -d "{ \"woof\": \"bark\"}" 发送 JSON 格式的 POST 报文

#一键运行远程 RVM 安装脚本

curl -sSL https://get.rvm.io | bash

#CURL 高级示例 (CURL Advanced)

命令指令 含义说明
curl -L -s http://ipecho.net/plain, curl -L -s http://whatismijnip.nl 获取当前出口公网 IP 地址
curl -u $username:$password http://repo.dennyzhang.com/README.txt 携带 HTTP 认证账号密码请求
curl -v -F key1=value1 -F upload=@localfilename <URL> 模拟表单上传本地文件
curl -k -v --http2 https://www.google.com/ 强制使用 HTTP/2 协议发起请求
curl -T cryptopp552.zip -u test:test ftp://10.32.99.187/ 通过 FTP 协议上传文件
curl -u test:test ftp://10.32.99.187/cryptopp552.zip -o cryptopp552.zip 通过 FTP 协议下载文件
curl -v -u admin:admin123 --upload-file package1.zip http://mysever:8081/dir/package1.zip 携带凭证通过 HTTP PUT 上传文件

#检查网站各阶段响应耗时

curl -s -w \
'\nLookup time:\t%{time_namelookup}\nConnect time:\t%{time_connect}\nAppCon time:\t%{time_appconnect}\nRedirect time:\t%{time_redirect}\nPreXfer time:\t%{time_pretransfer }\nStartXfer time:\t%{time_starttransfer}\n\nTotal time:\t%{time_total}\n' \
     -o /dev/null https://www.google.com

#检查远程文件资源状态码

curl -o /dev/null --silent -Iw "%{http_code}" https://example.com/my.remote.tarball.gz

#批量下载网站全部图片

curl https://example.com | \
grep --only-matching 'src="[^"]*.[png]"' | \
cut -d \" -f2 | \
while read i; do curl https://example.com/"${i}" \
-o "${i##*/}"; done

使用 GNU grep 下载页面上的所有 PNG 图片文件。

#按原始文件名下载保存

curl --remote-name "https://example.com/linux-distro.iso"

重命名输出保存

curl --remote-name "http://example.com/index.html" --output foo.html

#断点续传下载 (continue partial download)

curl --remote-name --continue-at -"https://example.com/linux-distro.iso"

#从多个域名并行/序列下载

curl "https://www.{example,w3,iana}.org/index.html" --output "file_#1.html"

#按数字编号批量下载文件序列

curl "https://{foo,bar}.com/file_[1-4].webp" --output "#1_#2.webp"

下载一系列连续编号的文件(输出为 foo_file1.webp, foo_file2.webp...bar_file1_webp 等)。

#重定向输出到本地文件

$ curl http://url/file > file

#基础 HTTP 认证 (Basic Authentication)

$ curl --user username:password http://example.com/
$ curl -u username:password http://example.com/

#保存输出到文件而非标准输出

$ curl -o file http://url/file
$ curl --output file http://url/file

#仅下载 HTTP 响应头信息

$ curl -I url
# 仅展示 Header 响应头

#指定写入远端保存文件名

$ curl -o file http://url/file
$ curl --output file http://url/file

#执行远程 Shell 脚本

$ curl -s http://url/myscript.sh

#读取配置文件 (Configuration file)

curl -K file
# 从配置文件中读取参数
curl --config file
$HOME/.curlrc # 类 UNIX 系统上的默认用户配置文件