skip to content
WangWindow

Search

    curl 是一个通用的网络请求客户端,支持 HTTP API、上传/下载、调试请求、认证、代理、多协议。功能十分强大,但是之前一般都是直接复制粘贴命令,没有仔细学习一下各个参数的具体含义,所以今天通过 GPT 帮我总结了一下

    使用 curl

    / 4 min read

    curl 与 wget 的比较

    工具类型擅长
    curl通用的网络请求客户端HTTP API、上传/下载、调试请求、认证、代理、多协议
    wget专门的下载器下载文件、递归下载网站、断点续传、镜像站点

    curl 的通用性主要体现在它不只是“下载文件”。例如:

    Terminal window
    # GET
    curl https://example.com
    # POST JSON
    curl -X POST https://example.com/api \
    -H 'Content-Type: application/json' \
    -d '{"name":"test"}'
    # 上传文件
    curl -F 'file=@a.png' https://example.com/upload
    # 带认证
    curl -u user:password https://example.com
    # SOCKS5 代理
    curl --proxy socks5h://127.0.0.1:1080 https://example.com

    日常使用上的差别:

    Terminal window
    wget https://example.com/a.zip

    默认就会保存为 a.zip

    但是 curl 默认把文件内容输出到终端

    Terminal window
    curl https://example.com/a.zip

    需要:

    Terminal window
    # 按远程文件名保存
    curl -O https://example.com/a.zip
    # 手动指定保存位置
    curl -o a.zip https://example.com/a.zip
    ⭐ curl 常用参数列表 ⭐
    参数作用示例
    -O按远程文件名保存curl -O https://example.com/a.zip
    -o FILE保存为指定文件名curl -o app.zip URL
    -L跟随重定向curl -L URL
    -C -断点续传curl -C - -O URL
    -s静默,不显示进度curl -s URL
    -S配合 -s,出错时仍显示错误curl -sS URL
    -fHTTP 4xx/5xx 时返回失败curl -f URL
    -I只获取响应头curl -I URL
    -i输出响应头 + 响应体curl -i URL
    -v显示详细请求过程curl -v URL
    -X指定 HTTP 方法curl -X POST URL
    -H添加请求头curl -H 'Authorization: Bearer xxx' URL
    -d发送请求数据curl -d 'a=1&b=2' URL
    --data-raw原样发送数据curl --data-raw '{"a":1}' URL
    -Fmultipart/form-data,常用于上传文件curl -F 'file=@a.png' URL
    -uBasic Authcurl -u user:pass URL
    -b发送 Cookiecurl -b 'token=abc' URL
    -c保存 Cookiecurl -c cookies.txt URL
    -A指定 User-Agentcurl -A 'Mozilla/5.0' URL
    -e指定 Referercurl -e 'https://google.com' URL
    -x / --proxy使用代理curl -x http://127.0.0.1:7890 URL
    --socks5SOCKS5 代理curl --socks5 127.0.0.1:1080 URL
    --socks5-hostnameSOCKS5,DNS 也通过代理curl --socks5-hostname 127.0.0.1:1080 URL
    -k忽略 HTTPS 证书错误curl -k https://example.com
    --connect-timeout连接超时curl --connect-timeout 5 URL
    -m整个请求最大时间curl -m 30 URL
    --retry请求失败自动重试curl --retry 5 URL
    -w输出请求统计信息`curl -w ’%{http_code}\n’ URL`

    下载功能说明

    下载功能主要与 -L 参数有关,同时支持与一些其他参数组合使用。

    通常使用:

    Terminal window
    curl -fsSL URL

    因为如果服务器返回 403404500-f 会让 curl 返回非 0 exit code。

    • -f HTTP 错误时失败
    • -s 静默
    • -S 静默模式下仍显示错误
    • -L 跟随重定向

    例如很多安装脚本都是:

    Terminal window
    curl -fsSL https://example.com/install.sh | sh

    不过对于不熟悉的脚本,建议先查看:

    Terminal window
    curl -fsSL https://example.com/install.sh | less

    确认内容后再执行。

    [!TIP]

    还支持断续传,添加 -C 参数以支持。

    网络请求功能

    主要与参数 -X-H-d 有关。

    一下是个示例:

    Terminal window
    curl -X POST https://api.example.com/users \
    -H 'Content-Type: application/json' \
    -d '{"name":"Alice","age":20}'

    测试网络代理

    通常完成环境变量设置后,使用:

    Terminal window
    export http_proxy=http://127.0.0.1:7890
    export https_proxy=http://127.0.0.1:7890
    curl https://www.google.com

    如果正常获得输出,这说明网络代理配置工作正常。