curl 使用手册

示例站点:https://www.bravexist.cn


目录


-v / --verbose

显示完整的请求与响应过程,包括 TLS 握手、请求头、响应头,适合调试。

1
curl -v https://www.bravexist.cn

输出示例(节选):

1
2
3
4
5
6
7
8
9
*   Trying 104.21.xx.xx:443...
* Connected to www.bravexist.cn (104.21.xx.xx) port 443
* SSL connection using TLSv1.3
> GET / HTTP/2
> Host: www.bravexist.cn
> User-Agent: curl/8.4.0
>
< HTTP/2 200
< content-type: text/html; charset=utf-8

> 表示发出的请求,< 表示收到的响应,* 是 curl 自身的信息。


-s / --silent

静默模式,不显示进度条和错误信息。常用于脚本中,避免多余输出。

1
curl -s https://www.bravexist.cn

单独使用 -s 时若请求失败不会有任何提示,建议配合 -S 一起用,保留错误输出:

1
curl -sS https://www.bravexist.cn

-o / --output

将响应体保存到文件,而不是打印到终端。

1
curl -o index.html https://www.bravexist.cn

配合 -s 使用,静默下载:

1
curl -s -o index.html https://www.bravexist.cn

下载文件并保留原始文件名(用 -O 大写):

1
curl -O https://www.bravexist.cn/logo.png

-i / --include

同时显示响应头和响应体。适合查看 Set-Cookie、Content-Type 等信息。

1
curl -i https://www.bravexist.cn

输出示例:

1
2
3
4
5
6
7
8
HTTP/2 200
content-type: text/html; charset=utf-8
server: cloudflare
...

<!DOCTYPE html>
<html>
...

-I / --head

只发送 HEAD 请求,只看响应头,不下载响应体。适合快速检查状态码、内容类型、服务器信息等。

1
curl -I https://www.bravexist.cn

输出示例:

1
2
3
4
5
HTTP/2 200
content-type: text/html; charset=utf-8
content-length: 12483
server: cloudflare
cf-ray: 8a1b2c3d4e5f-SIN

-H / --header

添加自定义请求头,可多次使用来添加多个头。

1
2
3
4
5
6
7
8
9
# 指定 Accept 类型
curl -H "Accept: application/json" https://www.bravexist.cn

# 发送 JSON 数据时设置 Content-Type
curl -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your_token" \
-d '{"key":"value"}' \
https://www.bravexist.cn/api

常见用途:

场景 示例
模拟 AJAX 请求 -H "X-Requested-With: XMLHttpRequest"
指定语言 -H "Accept-Language: zh-CN,zh;q=0.9"
携带 Token -H "Authorization: Bearer eyJhbGci..."
设置来源 -H "Referer: https://www.bravexist.cn"

-A / --user-agent

设置 User-Agent 字符串,模拟不同的浏览器或客户端。

1
2
3
4
5
6
7
8
9
10
# 模拟 Chrome 浏览器
curl -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/124.0 Safari/537.36" \
https://www.bravexist.cn

# 模拟 iPhone Safari
curl -A "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15" \
https://www.bravexist.cn

# 设置为空(移除 User-Agent)
curl -A "" https://www.bravexist.cn

默认 User-Agent 是 curl/版本号,某些网站会拦截它。


-u / --user

设置 HTTP 基本认证,格式为 用户名:密码

1
2
3
4
curl -u admin:password123 https://www.bravexist.cn/admin

# 只写用户名,curl 会交互式提示输入密码(更安全)
curl -u admin https://www.bravexist.cn/admin

会自动在请求头中加上:

1
Authorization: Basic YWRtaW46cGFzc3dvcmQxMjM=

密码以 Base64 编码传输,不是加密,务必配合 HTTPS 使用。


-x / --proxy

通过代理服务器发送请求,支持 HTTP、HTTPS、SOCKS5 代理。

1
2
3
4
5
6
7
8
# HTTP 代理
curl -x http://127.0.0.1:7890 https://www.bravexist.cn

# SOCKS5 代理
curl -x socks5://127.0.0.1:1080 https://www.bravexist.cn

# 带认证的代理
curl -x http://user:pass@proxy.example.com:8080 https://www.bravexist.cn

也可以通过环境变量设置,对所有 curl 命令生效:

1
2
export https_proxy=http://127.0.0.1:7890
curl https://www.bravexist.cn

-X / --request

指定 HTTP 请求方法,默认是 GET。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# GET(默认,可省略)
curl -X GET https://www.bravexist.cn

# POST 提交表单数据
curl -X POST -d "username=tom&password=123" https://www.bravexist.cn/login

# POST 提交 JSON
curl -X POST \
-H "Content-Type: application/json" \
-d '{"title":"Hello","body":"World"}' \
https://www.bravexist.cn/api/posts

# PUT 更新资源
curl -X PUT \
-H "Content-Type: application/json" \
-d '{"title":"Updated"}' \
https://www.bravexist.cn/api/posts/1

# DELETE 删除资源
curl -X DELETE https://www.bravexist.cn/api/posts/1

# PATCH 部分更新
curl -X PATCH \
-H "Content-Type: application/json" \
-d '{"status":"published"}' \
https://www.bravexist.cn/api/posts/1

使用 -d 发送数据时,curl 会自动将方法改为 POST,可省略 -X POST


-L / --location

自动跟随 HTTP 重定向(301、302 等)。默认情况下 curl 不会跟随跳转。

1
2
3
4
5
6
7
# 不加 -L,停在重定向处
curl https://bravexist.cn
# 输出:301 Moved Permanently

# 加 -L,自动跳转到最终地址
curl -L https://bravexist.cn
# 最终抵达 https://www.bravexist.cn 并返回页面内容

查看经历了哪些跳转(配合 -v):

1
curl -Lv https://bravexist.cn 2>&1 | grep -E "^[<>*]|Location"

限制最大重定向次数(默认 30 次):

1
curl -L --max-redirs 5 https://www.bravexist.cn

组合用法

调试请求,不保留响应体

1
curl -vs -o /dev/null https://www.bravexist.cn

静默下载并跟随重定向

1
curl -sLo page.html https://bravexist.cn

模拟浏览器发送 POST

1
2
3
4
5
6
curl -s -X POST \
-A "Mozilla/5.0 (Windows NT 10.0) Chrome/124.0" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your_token" \
-d '{"name":"bravexist"}' \
https://www.bravexist.cn/api/data

通过代理请求,显示响应头

1
curl -iL -x http://127.0.0.1:7890 https://www.bravexist.cn

检查网站是否可达(只看状态码)

1
curl -sLo /dev/null -w "%{http_code}" https://www.bravexist.cn

输出示例:200

常用 -w 输出格式变量

1
2
curl -sLo /dev/null -w "状态码: %{http_code}\n响应时间: %{time_total}s\n" \
https://www.bravexist.cn

速查表

参数 全称 作用
-v --verbose 显示详细请求/响应过程
-s --silent 静默,不显示进度和错误
-S --show-error -s 配合,保留错误输出
-o --output <file> 输出到指定文件
-O --remote-name 以远程文件名保存
-i --include 显示响应头 + 响应体
-I --head 只发 HEAD 请求
-H --header <header> 添加请求头
-A --user-agent <ua> 设置 User-Agent
-u --user <user:pass> HTTP 基本认证
-x --proxy <url> 使用代理
-X --request <method> 指定请求方法
-L --location 跟随重定向
-d --data <data> 发送请求体数据
-w --write-out <fmt> 自定义输出格式