【Linux】curl 基础语法与常用参数详解:从入门到实战
·
【Linux】curl 基础语法与常用参数详解:从入门到实战
前言
在 Linux 系统管理和后端开发中,curl 是一款不可或缺的网络传输工具。它支持多种协议(HTTP, HTTPS, FTP 等),能够灵活地模拟各种网络请求,是接口调试、自动化脚本编写以及服务器运维的利器。
本文将基于核心语法、常用参数、高级技巧及最佳实践,对 curl 进行系统化的整理,帮助你快速掌握其用法。
一、基础语法与核心结构
curl 的基本命令结构非常简洁:
curl [选项] <URL>
常用基础选项
| 参数 | 说明 | 适用场景 |
|---|---|---|
-v |
显示详细请求/响应日志(包含头部信息) | 调试接口,查看握手过程 |
-s |
静默模式(隐藏进度条和错误信息) | 脚本调用,只保留输出内容 |
-o |
将响应内容保存到指定文件 | 下载文件或保存 API 响应 |
-L |
跟随重定向(Handle redirects) | 访问短链接或发生 301/302 跳转时 |
示例:发送 GET 请求并查看详细信息
curl -v https://api.example.com/data
二、请求方法与数据提交
1. 定制请求方法
默认情况下 curl 发送 GET 请求。如需使用其他方法,需配合 -X 参数。
- POST 请求(携带 JSON 数据):
curl -X POST -d '{"key":"value"}' https://api.example.com/create - PUT/DELETE 请求:
curl -X PUT -d '{"id":1}' https://api.example.com/update curl -X DELETE https://api.example.com/resource/1 - GET 带查询参数:
直接拼接在 URL 后,建议用引号包裹以防特殊字符干扰。curl "https://api.example.com/search?q=test&page=1"
2. 请求头与认证机制
- 自定义请求头 (
-H):curl -H "Content-Type: application/json" \ -H "Authorization: Bearer token123" \ https://api.example.com/protected - Basic 认证 (
-u):curl -u username:password https://api.example.com/private - 动态 Token 认证:
结合命令替换获取 Token。curl -H "Authorization: Bearer $(oauth2-token)" https://api.example.com/secure
3. 数据格式处理
- JSON 请求体:
务必指定Content-Type,否则服务端可能无法解析。curl -X POST -H "Content-Type: application/json" \ -d '{"name":"John"}' \ https://api.example.com/users - 表单提交 (
--data-urlencode):
自动处理特殊字符编码,适合登录等场景。curl -X POST \ --data-urlencode "username=admin" \ --data-urlencode "password=123@456" \ https://api.example.com/login - 文件上传 (
-F):
模拟multipart/form-data表单上传。# 上传单个文件 curl -F "file=@/path/to/file.txt" http://example.com/upload # 上传多个文件 curl -F "avatar=@/path/to/avatar.jpg" \ -F "document=@/path/to/doc.pdf" \ http://example.com/upload # 指定文件名和 MIME 类型 curl -F "file=@/path/to/data.txt;filename=custom.txt;type=text/plain" \ http://example.com/upload
三、高级功能与实战技巧
1. 调试与模拟
- 捕获重定向:默认不跟随重定向,使用
-L强制跟随。curl -L https://bit.ly/example - 限制重定向次数:防止死循环。
curl -L --max-redirs 5 https://api.example.com/redirect-chain - 模拟浏览器:设置 User-Agent 和 Cookie 以绕过简单的反爬策略。
curl -H "User-Agent: Mozilla/5.0" \ -b "sessionid=abc123" \ https://api.example.com/profile
2. 性能优化与并发
- 并发请求测试:结合
xargs实现简单压测。seq 1 100 | xargs -n1 -P10 curl -s "https://api.example.com/test?id=" - 连接复用:减少 TCP 握手开销。
curl --keepalive-time 30 https://api.example.com/bulk
3. 响应处理自动化
- JSON 解析:管道配合
jq提取字段。curl -s https://api.example.com/data | jq '.results[0].id' - 错误处理逻辑:检查 HTTP 状态码。
if ! curl -s -o /dev/null -w "%{http_code}" https://api.example.com/health | grep -q "200"; then echo "API 服务异常" exit 1 fi
4. Cookie 管理
- 保存 Cookie:
curl -c cookies.txt https://api.example.com/login - 使用 Cookie:
curl -b cookies.txt https://api.example.com/dashboard
5. 超时与重试
- 超时设置:区分连接超时和总耗时。
curl --connect-timeout 10 --max-time 30 https://api.example.com/api - 失败重试:
curl --retry 3 --retry-delay 5 https://api.example.com/api
四、常见问题与解决方案
1. SSL 证书错误
在测试环境遇到自签名证书报错时:
- 跳过验证(仅限测试,生产环境慎用):
curl -k https://self-signed.example.com - 指定 CA 证书(推荐):
curl --cacert /path/to/cert.pem https://api.example.com
2. 代理配置
- HTTP 代理:
curl -x http://proxy.example.com:8080 https://api.example.com - SOCKS5 代理:
curl --socks5 127.0.0.1:1080 https://api.example.com
五、最佳实践建议
-
脚本化封装
将常用命令封装为 Shell 函数,提高复用性和可维护性。api_call() { curl -s -H "Authorization: Bearer $TOKEN" "$1" | jq . } api_call "https://api.example.com/users" -
日志记录
调试时将标准输出和错误输出重定向到文件。curl -v https://api.example.com > curl.log 2>&1 -
安全规范
- 避免明文密码:不要在命令行历史中直接暴露敏感信息,建议使用环境变量传递。
- 定期更新:保持
curl版本最新,以修复已知的安全漏洞。
结语
curl 功能强大且灵活,掌握其核心参数和组合技巧,能极大提升日常开发和运维效率。希望这篇整理能助你更优雅地使用 curl 解决实际问题。
更多推荐




所有评论(0)