curl 命令
1. 基本介绍
curl 是一个功能强大的命令行工具,用于在服务器之间传输数据。它支持多种协议,包括 HTTP、HTTPS、FTP、SFTP、TELNET 等,是开发人员和系统管理员常用的网络工具之一。
2. 基本语法
bash
curl [选项] [URL]
3. 常用选项
| 选项 | 描述 |
|---|---|
-A, --user-agent <agent> |
设置用户代理字符串 |
-b, --cookie <cookie> |
发送cookie到服务器 |
-c, --cookie-jar <file> |
将服务器返回的cookie保存到文件 |
-d, --data <data> |
发送POST请求的数据 |
-F, --form <name=content> |
发送表单数据 |
-H, --header <header> |
设置请求头 |
-i, --include |
显示响应头和响应体 |
-I, --head |
只显示响应头 |
-k, --insecure |
允许不安全的SSL连接 |
-L, --location |
跟随重定向 |
-o, --output <file> |
将输出保存到文件 |
-O, --remote-name |
将输出保存为远程文件名 |
-s, --silent |
静默模式,不显示进度信息 |
-u, --user <user:password> |
设置HTTP认证信息 |
-v, --verbose |
显示详细的请求和响应信息 |
-X, --request <method> |
指定HTTP请求方法 |
4. 常见用法示例
4.1 发送GET请求
bash
# 基本GET请求
curl https://example.com
# 显示响应头和响应体
curl -i https://example.com
# 只显示响应头
curl -I https://example.com
# 跟随重定向
curl -L https://example.com
4.2 发送POST请求
bash
# 发送表单数据
curl -d "username=admin&password=123456" https://example.com/login
# 发送JSON数据
curl -X POST -H "Content-Type: application/json" -d '{"name":"John","age":30}' https://example.com/api/users
# 发送文件内容
curl -d @data.txt https://example.com/upload
4.3 文件下载
bash
# 将输出保存到指定文件
curl -o output.txt https://example.com/file.txt
# 使用远程文件名保存
curl -O https://example.com/file.txt
# 断点续传
curl -C - -O https://example.com/large_file.zip
# 下载多个文件
curl -O https://example.com/file1.txt -O https://example.com/file2.txt
4.4 设置请求头
bash
# 设置单个请求头
curl -H "Content-Type: application/json" https://example.com/api
# 设置多个请求头
curl -H "Content-Type: application/json" -H "Authorization: Bearer token123" https://example.com/api
4.5 认证
bash
# HTTP基本认证
curl -u username:password https://example.com/secure
# Digest认证
curl --digest -u username:password https://example.com/secure
# Bearer令牌认证
curl -H "Authorization: Bearer token123" https://example.com/api
4.6 代理设置
bash
# HTTP代理
curl -x http://proxy.example.com:8080 https://example.com
# HTTPS代理
curl -x https://proxy.example.com:8443 https://example.com
# 带认证的代理
curl -x http://username:password@proxy.example.com:8080 https://example.com
4.7 其他常用功能
bash
# 测试连接速度
curl -w "Time: %{time_total}s\nSize: %{size_download} bytes\n" -o /dev/null -s https://example.com
# 限制下载速度
curl --limit-rate 100k -O https://example.com/large_file.zip
# 显示详细的请求和响应信息
curl -v https://example.com
# 发送PUT请求
curl -X PUT -d @file.txt https://example.com/api/resource
# 发送DELETE请求
curl -X DELETE https://example.com/api/resource/123
5. 高级用法
5.1 使用cookies
bash
# 发送cookies
curl -b "session_id=abc123; user=admin" https://example.com
# 保存cookies到文件
curl -c cookies.txt https://example.com
# 使用保存的cookies
curl -b cookies.txt https://example.com
5.2 发送文件上传表单
bash
# 上传文件
curl -F "file=@document.pdf" https://example.com/upload
# 带额外字段的文件上传
curl -F "name=John" -F "file=@document.pdf" https://example.com/upload
5.3 使用SSL/TLS
bash
# 允许不安全的SSL连接
curl -k https://example.com
# 指定CA证书
curl --cacert ca.crt https://example.com
# 使用客户端证书
curl --cert client.crt --key client.key https://example.com
6. 总结
curl 是一个功能强大且灵活的命令行工具,适用于各种网络数据传输场景。通过掌握其常用选项和用法,可以大大提高开发和系统管理的效率。
最后更新:2026-02-05