OA0 = Omni AI 0
OA0 是一个探索 AI 的论坛
现在注册
已注册用户请  登录
OA0  ›  技能包  ›  curl-http:用于 HTTP 请求、API 测试与文件下载的基础 curl 命令集

curl-http:用于 HTTP 请求、API 测试与文件下载的基础 curl 命令集

 
  mai ·  2026-02-20 00:24:01 · 3 次点击  · 0 条评论  

名称: curl-http
描述: 用于 HTTP 请求、API 测试和文件传输的常用 curl 命令。
主页: https://curl.se/
元数据: {"clawdbot":{"emoji":"🌐","requires":{"bins":["curl"]}}}


curl - HTTP 客户端

用于发起 HTTP 请求和传输数据的命令行工具。

基础请求

GET 请求

# 简单 GET 请求
curl https://api.example.com

# 将输出保存到文件
curl https://example.com -o output.html
curl https://example.com/file.zip -O  # 使用远程文件名

# 跟随重定向
curl -L https://example.com

# 显示响应头
curl -i https://example.com

# 仅显示响应头
curl -I https://example.com

# 详细输出(调试用)
curl -v https://example.com

POST 请求

# 发送数据的 POST 请求
curl -X POST https://api.example.com/users \
  -d "name=John&email=john@example.com"

# 发送 JSON 数据
curl -X POST https://api.example.com/users \
  -H "Content-Type: application/json" \
  -d '{"name":"John","email":"john@example.com"}'

# 从文件读取数据发送
curl -X POST https://api.example.com/users \
  -H "Content-Type: application/json" \
  -d @data.json

# 表单上传
curl -X POST https://api.example.com/upload \
  -F "file=@document.pdf" \
  -F "description=My document"

其他 HTTP 方法

# PUT 请求
curl -X PUT https://api.example.com/users/1 \
  -H "Content-Type: application/json" \
  -d '{"name":"Jane"}'

# DELETE 请求
curl -X DELETE https://api.example.com/users/1

# PATCH 请求
curl -X PATCH https://api.example.com/users/1 \
  -H "Content-Type: application/json" \
  -d '{"email":"newemail@example.com"}'

请求头与身份验证

自定义请求头

# 添加自定义请求头
curl -H "User-Agent: MyApp/1.0" https://example.com

# 多个请求头
curl -H "Accept: application/json" \
     -H "Authorization: Bearer token123" \
     https://api.example.com

身份验证

# 基础认证
curl -u username:password https://api.example.com

# Bearer Token
curl -H "Authorization: Bearer YOUR_TOKEN" \
  https://api.example.com

# API 密钥(放在请求头中)
curl -H "X-API-Key: your_api_key" \
  https://api.example.com

# API 密钥(放在 URL 中)
curl "https://api.example.com?api_key=your_key"

高级功能

超时与重试

# 连接超时(秒)
curl --connect-timeout 10 https://example.com

# 整个操作的最大时间
curl --max-time 30 https://example.com

# 失败时重试
curl --retry 3 https://example.com

# 重试延迟
curl --retry 3 --retry-delay 5 https://example.com
# 发送 Cookie
curl -b "session=abc123" https://example.com

# 将 Cookie 保存到文件
curl -c cookies.txt https://example.com

# 从文件加载 Cookie
curl -b cookies.txt https://example.com

# 同时保存和加载 Cookie
curl -b cookies.txt -c cookies.txt https://example.com

代理

# 使用 HTTP 代理
curl -x http://proxy.example.com:8080 https://api.example.com

# 带代理认证
curl -x http://proxy:8080 -U user:pass https://api.example.com

# SOCKS 代理
curl --socks5 127.0.0.1:1080 https://api.example.com

SSL/TLS

# 忽略 SSL 证书错误(生产环境不推荐)
curl -k https://self-signed.example.com

# 使用特定 SSL 版本
curl --tlsv1.2 https://example.com

# 使用客户端证书
curl --cert client.crt --key client.key https://example.com

# 显示 SSL 握手详情
curl -v https://example.com 2>&1 | grep -i ssl

响应处理

输出格式化

# 静默模式(无进度条)
curl -s https://api.example.com

# 仅显示 HTTP 状态码
curl -s -o /dev/null -w "%{http_code}" https://example.com

# 自定义输出格式
curl -w "\nTime: %{time_total}s\nStatus: %{http_code}\n" \
  https://example.com

# 美化 JSON 输出(需 jq)
curl -s https://api.example.com | jq '.'

范围请求

# 下载指定字节范围
curl -r 0-1000 https://example.com/large-file.zip

# 断点续传
curl -C - -O https://example.com/large-file.zip

文件操作

下载文件

# 下载文件
curl -O https://example.com/file.zip

# 使用自定义文件名下载
curl -o myfile.zip https://example.com/file.zip

# 下载多个文件
curl -O https://example.com/file1.zip \
     -O https://example.com/file2.zip

# 恢复中断的下载
curl -C - -O https://example.com/large-file.zip

上传文件

# FTP 上传
curl -T file.txt ftp://ftp.example.com/upload/

# HTTP PUT 上传
curl -T file.txt https://example.com/upload

# 表单文件上传
curl -F "file=@document.pdf" https://example.com/upload

测试与调试

API 测试

# 测试 REST API
curl -X GET https://api.example.com/users
curl -X GET https://api.example.com/users/1
curl -X POST https://api.example.com/users -d @user.json
curl -X PUT https://api.example.com/users/1 -d @updated.json
curl -X DELETE https://api.example.com/users/1

# 带详细输出的测试
curl -v -X POST https://api.example.com/login \
  -H "Content-Type: application/json" \
  -d '{"username":"test","password":"pass"}'

性能测试

# 测量请求时间
curl -w "Total time: %{time_total}s\n" https://example.com

# 详细计时信息
curl -w "\nDNS: %{time_namelookup}s\nConnect: %{time_connect}s\nTLS: %{time_appconnect}s\nTransfer: %{time_starttransfer}s\nTotal: %{time_total}s\n" \
  -o /dev/null -s https://example.com

常用调试命令

# 显示请求和响应头
curl -v https://api.example.com

# 跟踪请求详情
curl --trace-ascii trace.txt https://api.example.com

# 在输出中包含响应头
curl -i https://api.example.com

常用模式

快速测试 JSON API:

curl -s https://api.github.com/users/octocat | jq '{name, bio, followers}'

带进度条下载:

curl -# -O https://example.com/large-file.zip

发送 JSON 并提取字段:

curl -s -X POST https://api.example.com/login \
  -H "Content-Type: application/json" \
  -d '{"user":"test","pass":"secret"}' | jq -r '.token'

检查 URL 是否可访问:

if curl -s --head --fail https://example.com > /dev/null; then
  echo "网站正常"
else
  echo "网站异常"
fi

并行下载:

for i in {1..10}; do
  curl -O https://example.com/file$i.jpg &
done
wait

常用参数

  • -X: 指定 HTTP 方法(GET、POST、PUT、DELETE 等)
  • -d: 发送的数据(用于 POST/PUT)
  • -H: 自定义请求头
  • -o: 指定输出文件
  • -O: 使用远程文件名保存
  • -L: 跟随重定向
  • -i: 在输出中包含响应头
  • -I: 仅获取响应头
  • -v: 详细输出
  • -s: 静默模式
  • -S: 静默模式下仍显示错误
  • -f: HTTP 错误时静默失败
  • -k: 不安全模式(忽略 SSL 错误)
  • -u: 基础认证
  • -F: 多部分表单数据
  • -b: 发送 Cookie
  • -c: 保存 Cookie
  • -w: 自定义输出格式

实用技巧

  • 在脚本中使用 -s 来隐藏进度条
  • 组合使用 -sS 实现静默但显示错误
  • 处理重定向时使用 -L(例如短链接)
  • 调试时添加 -v 参数
  • 使用 jq 处理 JSON 响应
  • 将常用请求保存为 shell 别名或脚本
  • 使用 --config 处理复杂的可复用请求

文档

官方文档:https://curl.se/docs/
手册:man curl
HTTP 方法:https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Methods

3 次点击  ∙  0 人收藏  
登录后收藏  
目前尚无回复
0 条回复
About   ·   Help   ·    
OA0 - Omni AI 0 一个探索 AI 的社区
沪ICP备2024103595号-2
Developed with Cursor