# curl 命令
curl
命令是一个用于传输数据的工具,支持多种协议(HTTP、HTTPS、FTP等)。
# 语法
curl [选项] URL
# 常用选项
-o, --output <file>
: 将输出写入文件-O, --remote-name
: 使用远程文件名保存文件-L, --location
: 跟随重定向-I, --head
: 仅显示响应头信息-X, --request <command>
: 指定请求方法-H, --header <header>
: 添加请求头-d, --data <data>
: 发送POST数据-v, --verbose
: 显示详细信息
# 使用示例
# HTTP GET 请求
# 基本GET请求
curl https://api.example.com/data
# 保存响应到文件
curl -o output.json https://api.example.com/data
# 显示响应头
curl -I https://example.com
# HTTP POST 请求
# 发送POST请求
curl -X POST -d "name=value" https://api.example.com/create
# 发送JSON数据
curl -X POST \
-H "Content-Type: application/json" \
-d '{"key":"value"}' \
https://api.example.com/create
# 文件下载
# 下载文件并保存
curl -O https://example.com/file.zip
# 继续中断的下载
curl -C - -O https://example.com/file.zip
# 认证
# 基本认证
curl -u username:password https://api.example.com
# Bearer Token认证
curl -H "Authorization: Bearer token" https://api.example.com
# 常见用途
- API测试
- 文件下载
- 网站监控
- 数据传输
# 调试技巧
使用
-v
查看详细信息curl -v https://api.example.com
保存响应头和体
curl -D headers.txt -o body.txt https://api.example.com
# 注意事项
- 安全性
- 避免在命令行中明文传递敏感信息
- 使用HTTPS进行安全传