一、curl基础语法与常用参数详解

1. 基础请求结构

curl [选项] <URL>

示例:发送GET请求并打印响应头与体

curl -v https://api.example.com/data
  • -v:显示详细请求/响应日志(调试必备)。
  • -s:静默模式(隐藏进度信息,适合脚本调用)。
  • -o:将响应保存到文件(如-o output.json)。

2. 请求方法定制

  • POST请求:通过 -x 指定方法,-d 传递数据

    curl -X POST -d '{"key":"value"}' https://api.example.com/create
    
  • PUT/DELETE请求:同理替换-X参数。

  • GET带查询参数:直接拼接在URL后

    curl "https://api.example.com/search?q=test&page=1"
    

3. 请求头与认证

  • 自定义请求头:使用 -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
    
  • Bearer Token:常见于OAuth2.0认证

    curl -H "Authorization: Bearer $(oauth2-token)" https://api.example.com/secure
    

4. 数据格式处理

  • 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模拟多部分表单

    curl -X POST -F "file=@/path/to/file.txt" 	-F "description=test" https://api.example.com/upload
    
    # 上传单个文件
    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. 调试复杂接口

  • 捕获重定向:默认curl会跟随3xx重定向,使用 -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. 性能优化与并发

  • 并发请求:结合GNU Parallel或xargs实现压力测试

    seq 1 100 | xargs -n1 -P10 curl -s "https://api.example.com/test?id="
    
  • 连接复用:通过--keepalive-time减少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.保存和重放请求

# 保存请求到文件
curl -v https://api.example.com/users > request.txt 2>&1

# 从文件读取请求数据
curl -X POST https://api.example.com/users \
  -H "@headers.txt" \
  -d "@data.json"

5.处理cookie

# 保存 Cookie
curl -c cookies.txt https://api.example.com/login

# 使用 Cookie
curl -b cookies.txt https://api.example.com/dashboard

6.超时和重试设置

# 设置超时
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证书错误

  • 跳过验证

    (仅测试环境):添加-k参数

    curl -k https://self-signed.example.com
    
  • 指定CA证书:使用--cacert

    curl --cacert /path/to/cert.pem https://api.example.com
    

2、超时与重试

  • 设置超时:--connect-timeout(连接阶段)和--max-time(总时间)

    curl --connect-timeout 10 --max-time 30 https://api.example.com/slow
    
  • 自动重试:通过脚本封装实现

    for i in {1..3}; do
    	curl -s https://api.example.com/retry && break || sleep 1
    done
    

3. 代理与网络限制

  • 配置HTTP代理:

    curl -x http://proxy.example.com:8080 https://api.example.com
    
  • SOCKS5代理:

    curl --socks5 127.0.0.1:1080 https://api.example.com
    

五、最佳实践建议

  1. 脚本化封装:将常用curl命令封装为Shell函数,提升复用性。
api_call() {  
	curl -s -H "Authorization: Bearer $TOKEN" "$1" | jq .
}
api_call "https://api.example.com/users"
  1. 日志记录:重定向输出到文件,便于问题追踪。

    curl -v https://api.example.com > curl.log 2>&1
    
  2. 安全规范:

    • 避免在命令行中直接暴露敏感信息(如密码),建议通过环境变量传递。

    • 定期更新curl版本以修复安全漏洞。

 posted on 2025-11-17 00:48  二月无雨  阅读(2)  评论(0)    收藏  举报