linux服务器curl命令常用操作
我来详细介绍 curl 这个强大的命令行工具的主要用法。
基本 GET 请求
# 最简单的 GET 请求
curl https://example.com
# 保存响应到文件
curl -o output.html https://example.com
1
2
3
4
5
2
3
4
5
发送 POST 请求
# 发送表单数据
curl -X POST -d "name=john&age=25" https://example.com/api
# 发送 JSON 数据
curl -X POST \
-H "Content-Type: application/json" \
-d '{"name":"john","age":25}' \
https://example.com/api
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
自定义请求头
# 添加单个请求头
curl -H "Authorization: Bearer token123" https://api.example.com
# 添加多个请求头
curl -H "Content-Type: application/json" \
-H "Authorization: Bearer token123" \
https://api.example.com
1
2
3
4
5
6
7
2
3
4
5
6
7
处理认证
# 基本认证
curl -u username:password https://example.com
# 仅提供用户名(密码将通过提示输入)
curl -u username https://example.com
1
2
3
4
5
2
3
4
5
文件上传
# 上传单个文件
curl -F "file=@photo.jpg" https://example.com/upload
# 上传多个文件
curl -F "file1=@file1.txt" -F "file2=@file2.txt" https://example.com/upload
1
2
3
4
5
2
3
4
5
调试选项
# 显示详细的通信过程
curl -v https://example.com
# 仅显示响应头
curl -I https://example.com
# 显示请求时间统计
curl -w "\nTime: %{time_total}s\n" https://example.com
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
其他常用选项
# 允许不安全的 SSL 连接
curl -k https://example.com
# 使用代理
curl -x proxy.example.com:8080 https://example.com
# 跟随重定向
curl -L https://example.com
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
下载文件
# 下载文件并显示进度条
curl -# -O https://example.com/file.zip
# 断点续传
curl -C - -O https://example.com/file.zip
#Markdown 格式的单个 -# 和 -O 之间不应该有空格。此外,针对 GitHub Release 文件下载,必须添加 -L 参数来处理重定向
1
2
3
4
5
6
7
2
3
4
5
6
7
一些实际场景的具体示例。
场景1: 调用 REST API
# 获取用户信息
curl -X GET \
-H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..." \
-H "Content-Type: application/json" \
https://api.example.com/users/123
# 创建新用户
curl -X POST \
-H "Content-Type: application/json" \
-d '{
"username": "alice",
"email": "alice@example.com",
"age": 28
}' \
https://api.example.com/users
# 更新用户信息
curl -X PUT \
-H "Content-Type: application/json" \
-d '{
"email": "newalice@example.com",
"age": 29
}' \
https://api.example.com/users/123
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
场景2: 文件上传到云存储
# 上传图片到服务器并添加元数据
curl -X POST \
-F "image=@/path/to/photo.jpg" \
-F "title=My Vacation" \
-F "tags=summer,beach,2024" \
https://storage.example.com/upload
# 上传多个文件并显示进度
curl -X POST \
-F "files[]=@doc1.pdf" \
-F "files[]=@doc2.pdf" \
-F "files[]=@doc3.pdf" \
-F "folder=reports" \
--progress-bar \
https://storage.example.com/batch-upload
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
场景3: 网站登录和会话管理
# 登录并保存 Cookie
curl -X POST \
-c cookies.txt \
-d "username=admin&password=secret123" \
https://example.com/login
# 使用保存的 Cookie 访问受保护页面
curl -b cookies.txt \
https://example.com/dashboard
# 模拟完整的登录流程
curl -X POST \
-c cookies.txt \
-b cookies.txt \
-L \
-d "username=admin&password=secret123" \
--referer "https://example.com/login" \
-H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64)" \
https://example.com/login
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
场景4: 下载管理
# 下载大文件并显示进度条
curl -# \
-C - \
-o "ubuntu-20.04.iso" \
http://releases.ubuntu.com/20.04/ubuntu-20.04-desktop-amd64.iso
# 批量下载文件
for i in {1..5}; do
curl -O "https://example.com/files/file${i}.txt"
done
# 下载并自动重命名
curl -O -J -L "https://example.com/download/report.pdf"
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
场景5: API 测试和调试
# 测试 API 响应时间
curl -w "\nDNS解析时间: %{time_namelookup}s\n连接时间: %{time_connect}s\n总时间: %{time_total}s\n" \
-o /dev/null -s \
https://api.example.com/endpoint
# 获取完整的 HTTP 交互信息
curl -v \
-H "Authorization: Bearer token123" \
https://api.example.com/users \
2>&1 | tee api-debug.log
# 测试 API 错误处理
curl -i \
-H "Content-Type: application/json" \
-d '{"invalid": "data"}' \
https://api.example.com/validate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
场景6: 压力测试
# 简单的并发请求测试
for i in {1..10}; do
curl -o /dev/null -s -w "%{http_code}\n" https://example.com &
done
# 带延迟的重复请求
for i in {1..5}; do
curl -s https://example.com/api
sleep 2
done
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
场景7: 代理和 VPN 使用
# 使用 SOCKS5 代理
curl -x socks5://localhost:1080 \
https://example.com
# 通过 HTTP 代理访问 HTTPS 站点
curl -x http://proxy.example.com:8080 \
https://secure.example.com
# 使用代理并进行认证
curl -x http://username:password@proxy.example.com:8080 \
https://example.com
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
这些示例展示了在实际工作中常见的使用场景。每个命令都可以根据具体需求进行调整和组合。如果您对某个特定场景感兴趣,我可以提供更详细的说明和变体。


上次更新: 2025/01/13, 10:29:17