1. curl 简介

  • 定义curl(Client URL)是一个开源的命令行工具和跨平台库(libcurl),用于通过 URL 语法在网络上传输数据。
  • 作者:Daniel Stenberg(1996 年首次发布)。
  • 特点
    • 支持 30+ 协议(HTTP/HTTPS, FTP, FTPS, SCP, SFTP, SMTP, IMAP, MQTT, LDAP 等)。
    • 跨平台(Windows、Linux、macOS)。
    • 无交互式界面,适合自动化脚本。
    • 提供详细的调试和日志功能。

2. 核心功能

协议支持

协议 用途示例
HTTP/HTTPS API 调用、网页下载
FTP/FTPS 文件上传/下载
SFTP/SCP 通过 SSH 安全传输文件
SMTP/IMAP 发送邮件、读取邮箱
RTSP 流媒体控制
GOPHER 访问 Gopher 协议资源(复古用途)

核心参数详解

通用选项
参数 说明
-X <METHOD> 指定 HTTP 方法(如 GET, POST, PUT, DELETE)。
-H "Header: Val" 添加自定义请求头(如 -H "User-Agent: MyApp")。
-d "data" 发送 POST 请求的正文数据(默认 Content-Type: application/x-www-form-urlencoded)。
-F "name=@file" 上传文件(multipart/form-data 格式)。
-u user:pass 基本认证或协议登录(如 FTP)。
-k 跳过 SSL 证书验证(不安全,仅用于测试)。
-L 自动跟随重定向(处理 3xx 响应)。
-o <file> 将输出保存到指定文件。
-O 保存文件时使用远程文件名。
-v 显示详细请求/响应过程(调试模式)。
-s 静默模式(隐藏进度和错误信息)。
--limit-rate 200K 限制传输速度为 200KB/s。

高级选项
参数 说明
-A "User Agent" 伪装客户端类型(如 -A "Mozilla/5.0")。
-b <cookiefile> 从文件读取 Cookie(如 -b cookies.txt)。
-c <cookiefile> 将 Cookie 保存到文件。
--compressed 请求压缩响应(支持 gzip、brotli)。
-C - 断点续传(自动从上次中断处继续下载)。
--retry <num> 失败时重试次数(如 --retry 3)。
--proxy <proxy_url> 使用代理服务器(支持 HTTP/SOCKS)。
-J 从响应头解析文件名并保存。
--data-urlencode 对数据进行 URL 编码后发送(常用于 GET 参数)。

3. 实际应用场景

场景 1:API 调试

# 发送带 Bearer Token 的 GET 请求
curl -H "Authorization: Bearer YOUR_TOKEN" https://api.example.com/data

# 提交 JSON 数据到 REST API
curl -X POST \
  -H "Content-Type: application/json" \
  -d '{"username": "admin", "password": "secret"}' \
  https://api.example.com/login

# 上传文件到 API(multipart/form-data)
curl -F "file=@/path/to/image.jpg" -F "description=My Photo" https://api.example.com/upload

场景 2:文件传输

# 从 FTP 下载文件(带认证)
curl -u ftp_user:ftp_password ftp://example.com/file.zip -O

# 上传文件到 SFTP
curl -u sftp_user --key ~/.ssh/id_rsa sftp://example.com/upload/ -T local_file.txt

# 批量下载(配合循环)
for url in $(cat urls.txt); do curl -O $url; done

场景 3:邮件操作

# 发送邮件(需配置 SMTP 服务器)
curl smtp://smtp.example.com:587 \
  --mail-from "sender@example.com" \
  --mail-rcpt "receiver@example.com" \
  --ssl -u user:password \
  -T email.txt

email.txt 内容

From: Sender <sender@example.com>
To: Receiver <receiver@example.com>
Subject: Test Email

Hello, this is a test email sent via curl!

场景 4:高级调试

# 跟踪完整请求过程(包含 TCP 握手、SSL 协商)
curl --trace-ascii debug.txt https://example.com

# 仅测量请求时间(不下载内容)
curl -w "DNS解析: %{time_namelookup}\n连接时间: %{time_connect}\n总时间: %{time_total}\n" -o /dev/null -s https://example.com

4. 高级技巧

1. 处理认证

  • OAuth 2.0
    curl -H "Authorization: Bearer $(gcloud auth print-access-token)" https://api.example.com
    
  • API 密钥
    curl https://api.example.com?api_key=YOUR_KEY
    

2. 处理大文件

  • 分块下载
    curl -r 0-999999 -o chunk1 https://example.com/largefile
    curl -r 1000000-1999999 -o chunk2 https://example.com/largefile
    
  • 压缩传输
    curl --compressed -O https://example.com/data.json.gz
    

3. 脚本集成

  • 解析 JSON 响应
    curl -s https://api.example.com/data | jq '.results[0].name'
    
  • 自动化测试
    if curl -s --head https://example.com | grep "200 OK"; then
      echo "Server is up!"
    else
      echo "Server is down!"
    fi
    

5. 安全注意事项

  1. 避免明文密码
    # 使用 .netrc 文件保存凭证
    echo "machine example.com login user password pass" >> ~/.netrc
    curl -n https://example.com
    
  2. 验证 SSL 证书
    • 默认启用证书验证,避免使用 -k 跳过。
  3. 敏感信息隐藏
    curl -H "Authorization: $(cat api_key.txt)" https://api.example.com
    

6. 常见问题解决

  • 超时处理
    curl --connect-timeout 10 --max-time 30 https://example.com
    
  • 代理配置
    curl -x http://proxy.example.com:8080 https://target.example.com
    
  • 编码问题
    curl --data-urlencode "query=hello world" https://example.com/search
    

7. 扩展工具

  • httpie:更友好的 HTTP 客户端。
  • jq:JSON 数据处理工具。
  • wget:类似工具,更适合递归下载。

8. 资源

Logo

这里是“一人公司”的成长家园。我们提供从产品曝光、技术变现到法律财税的全栈内容,并连接云服务、办公空间等稀缺资源,助你专注创造,无忧运营。

更多推荐