【Linux】之 curl 命令:从基础到实战,解锁网络调试与自动化
1. 初识curl:命令行中的瑞士军刀
第一次接触curl是在调试API接口的时候,同事轻描淡写地在终端敲了一行命令,瞬间就拿到了服务器返回的JSON数据。当时我就震惊了——原来不用打开Postman,不用写代码,一个简单的命令就能完成HTTP请求!
curl的全称是"Client for URLs",诞生于1998年,由Daniel Stenberg开发。这个看似简单的工具支持超过25种网络协议,包括HTTP、HTTPS、FTP等,几乎能完成所有与网络传输相关的操作。我经常把它比作命令行界的瑞士军刀,因为它确实太全能了:
- 无界面操作 :纯命令行工具,特别适合自动化脚本
- 跨平台支持 :Linux、macOS、Windows通吃
- 功能丰富 :文件传输、API测试、性能监控样样精通
- 开发利器 :调试接口时比图形化工具更高效
安装curl非常简单,大多数Linux发行版已经预装。如果没有,用包管理器一键安装:
# Ubuntu/Debian
sudo apt install curl
# CentOS/RHEL
sudo yum install curl
2. 基础用法:HTTP请求的四种姿势
2.1 GET请求:获取数据最简单的方式
GET是最基础的HTTP方法,用curl发送GET请求有三种写法:
# 最简形式(隐式GET)
curl https://api.example.com/data
# 显式指定GET方法
curl -X GET https://api.example.com/data
# 带查询参数的GET(参数需要URL编码)
curl -G https://api.example.com/search --data-urlencode "q=关键字"
上周排查一个生产环境问题时,我就是用 curl -v https://api.example.com/status 快速确认了服务是否存活,-v参数让我能看到完整的请求头和响应头,非常方便。
2.2 POST请求:提交数据的艺术
POST请求常用于提交表单或JSON数据。这里有个坑要注意:当使用-d参数时,curl会自动加上 Content-Type: application/x-www-form-urlencoded 头。如果需要发送JSON,必须手动指定Content-Type:
# 表单提交
curl -d "username=admin&password=123456" https://api.example.com/login
# JSON提交(必须指定Content-Type)
curl -X POST -H "Content-Type: application/json" \
-d '{"username":"admin","password":"123456"}' \
https://api.example.com/login
# 从文件读取JSON数据
curl -X POST -H "Content-Type: application/json" \
-d @data.json \
https://api.example.com/login
2.3 PUT与DELETE:资源操作双雄
RESTful API中常用的两个方法:
# 更新资源
curl -X PUT -d "title=新标题" https://api.example.com/posts/1
# 删除资源
curl -X DELETE https://api.example.com/posts/1
曾经踩过一个坑:某API的DELETE操作需要传空JSON体,必须显式指定 -d '{}' ,否则会返回400错误。
2.4 处理响应:保存与格式化
默认curl会把响应输出到终端,但我们可以控制输出:
# 保存到文件(自动使用远程文件名)
curl -O https://example.com/file.zip
# 指定本地文件名
curl -o local.zip https://example.com/file.zip
# 格式化JSON输出(需要jq工具)
curl https://api.example.com/data | jq .
# 只显示HTTP状态码
curl -o /dev/null -s -w "%{http_code}\n" https://api.example.com
3. 高级技巧:解锁专业用法
3.1 会话保持:Cookie的妙用
很多Web应用依赖Cookie保持会话,curl也能完美支持:
# 登录并保存Cookie到文件
curl -c cookies.txt -d "user=admin&pass=123" https://example.com/login
# 使用保存的Cookie访问受限页面
curl -b cookies.txt https://example.com/dashboard
# 同时发送多个Cookie
curl -b "sessionid=abc123; theme=dark" https://example.com
3.2 请求头控制:伪装的艺术
调试时经常需要修改请求头:
# 设置User-Agent伪装成浏览器
curl -A "Mozilla/5.0" https://example.com
# 自定义多个请求头
curl -H "Accept: application/json" \
-H "X-Request-ID: 12345" \
https://api.example.com
# 删除默认的User-Agent头
curl -H "User-Agent:" https://example.com
3.3 文件传输:上传下载一条龙
curl不仅是HTTP客户端,还是强大的文件传输工具:
# 上传文件(multipart/form-data)
curl -F "[email protected]" https://example.com/upload
# FTP下载
curl -u ftpuser:ftppass -O ftp://example.com/file.zip
# 断点续传
curl -C - -O https://example.com/bigfile.iso
# 限速下载(100KB/s)
curl --limit-rate 100K -O https://example.com/largefile.mp4
4. 实战场景:从调试到自动化
4.1 API测试与调试
我每天都会用curl测试API,这些参数组合特别实用:
# 查看请求详情(调试SSL问题特别有用)
curl -v https://api.example.com
# 只显示响应头
curl -I https://api.example.com
# 测量请求时间
curl -w "DNS解析: %{time_namelookup}\n连接建立: %{time_connect}\n首字节: %{time_starttransfer}\n总时间: %{time_total}\n" \
https://api.example.com
4.2 网页抓取与处理
虽然不如专业爬虫强大,但curl配合其他工具也能完成简单抓取:
# 获取网页内容并提取所有链接
curl -s https://example.com | grep -o 'href="[^"]*"'
# 下载整站(谨慎使用!)
curl -r 0-1000000 -o partial.html https://example.com
4.3 自动化脚本集成
在Shell脚本中,curl是处理HTTP请求的首选:
#!/bin/bash
# 获取天气API数据并提取温度
temp=$(curl -s https://api.weather.com/data | jq '.temperature')
echo "当前温度: $temp°C"
# 定时监控网站状态
while true; do
status=$(curl -o /dev/null -s -w "%{http_code}" https://example.com)
[ "$status" != "200" ] && echo "网站异常: $status" | mail -s "告警" admin@example.com
sleep 60
done
5. 避坑指南:常见问题解决
5.1 SSL证书问题
遇到证书错误时,可以临时跳过验证(生产环境不推荐):
# 跳过SSL验证(不安全!仅用于测试)
curl -k https://example.com
# 指定CA证书
curl --cacert /path/to/ca.pem https://example.com
5.2 重定向处理
默认curl不会跟随重定向,需要-L参数:
# 跟随重定向(最多5次)
curl -L --max-redirs 5 https://example.com
# 不跟随重定向但显示重定向信息
curl -v https://example.com
5.3 连接超时设置
对于不稳定网络,合理设置超时很重要:
# 连接超时10秒,传输超时30秒
curl --connect-timeout 10 --max-time 30 https://example.com
# 重试3次,间隔5秒
curl --retry 3 --retry-delay 5 https://example.com
6. 性能优化:让curl飞起来
6.1 连接复用
通过保持TCP连接减少握手开销:
# 启用keep-alive
curl -H "Connection: keep-alive" https://api.example.com
# 并行下载多个文件
curl -Z -O https://example.com/file1.zip -O https://example.com/file2.zip
6.2 压缩传输
减少数据传输量:
# 请求gzip压缩内容
curl -H "Accept-Encoding: gzip" --compressed https://example.com
6.3 DNS缓存
对于频繁请求同一域名:
# 使用本地DNS缓存(需要nscd服务)
curl --dns-servers 8.8.8.8 https://example.com
7. 安全实践:保护你的请求
7.1 敏感信息处理
不要在命令行直接暴露密码:
# 从环境变量读取密码
curl -u $API_USER:$API_PASS https://api.example.com
# 从文件读取token
curl -H "Authorization: $(cat token.txt)" https://api.example.com
7.2 请求签名
对重要请求进行签名:
# 生成签名
timestamp=$(date +%s)
signature=$(echo -n "$timestamp$SECRET" | sha256sum | cut -d' ' -f1)
# 带签名的请求
curl -H "X-Timestamp: $timestamp" \
-H "X-Signature: $signature" \
https://api.example.com/sensitive
8. 扩展应用:curl的创意用法
8.1 网络诊断
# 测试端口连通性
curl -v telnet://example.com:22
# 获取公网IP
curl ifconfig.me
8.2 与jq配合处理JSON
# 提取JSON中的特定字段
curl -s https://api.example.com/users | jq '.[] | select(.age > 30)'
# 格式化并高亮JSON输出
curl -s https://api.example.com/data | jq . --color-output
8.3 制作简单HTTP服务
# 快速共享当前目录文件
python3 -m http.server 8000
# 然后另一台机器用curl下载
curl http://your-ip:8000/file.txt
更多推荐




所有评论(0)