简介

curl(Client URL)是一个用于传输数据的命令行工具,支持多种协议(HTTP、HTTPS、FTP等),是开发者和系统管理员进行网络操作的得力助手。

主要功能

  • 发送HTTP请求
  • 下载和上传文件
  • API测试和调试
  • 网站可用性检查
  • 支持多种协议和认证方式

基本用法

基本请求

# 发送GET请求
curl https://api.example.com

# 保存输出到文件
curl -o output.html https://example.com

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

# 显示完整的请求和响应
curl -v https://example.com

常用选项

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

# 静默模式
curl -s https://example.com

# 显示进度条
curl -# https://example.com

# 设置超时时间
curl --connect-timeout 10 https://example.com

HTTP方法

GET请求

# 基本GET请求
curl https://api.example.com

# 带参数的GET请求
curl "https://api.example.com/search?q=test&page=1"

# 使用-G选项构造查询参数
curl -G -d "q=test" -d "page=1" https://api.example.com/search

POST请求

# 发送表单数据
curl -X POST -d "name=john&age=25" https://api.example.com/users

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

# 发送文件内容
curl -X POST -d @data.json https://api.example.com/users

其他HTTP方法

# PUT请求
curl -X PUT -d "data" https://api.example.com/users/1

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

# PATCH请求
curl -X PATCH -d "data" https://api.example.com/users/1

# OPTIONS请求
curl -X OPTIONS https://api.example.com

请求头和数据

自定义请求头

# 添加单个请求头
curl -H "Authorization: Bearer token123" https://api.example.com

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

# 添加Cookie
curl -b "session=123" https://example.com

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

发送数据

# 发送URL编码数据
curl -d "param1=value1¶m2=value2" https://api.example.com

# 发送JSON数据
curl -d '{"key":"value"}' \
     -H "Content-Type: application/json" \
     https://api.example.com

# 上传文件
curl -F "file=@photo.jpg" https://api.example.com/upload

# 发送原始数据
curl --data-raw "raw data" https://api.example.com

认证和安全

基本认证

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

# 提示输入密码
curl -u username https://api.example.com

# 使用.netrc文件
curl -n https://api.example.com

SSL/TLS选项

# 忽略SSL证书验证
curl -k https://example.com

# 指定客户端证书
curl --cert client.pem https://example.com

# 指定私钥
curl --cert client.pem --key key.pem https://example.com

# 指定CA证书
curl --cacert ca.pem https://example.com

高级特性

代理设置

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

# 使用SOCKS5代理
curl --socks5 proxy.example.com:1080 https://api.example.com

# 指定代理认证
curl -x proxy.example.com:8080 -U username:password https://api.example.com

高级传输选项

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

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

# 使用通配符
curl -O https://example.com/file[1-10].zip

# 指定范围下载
curl -r 0-1024 https://example.com/file.zip

调试技巧

请求调试

# 显示详细信息
curl -v https://api.example.com

# 仅显示请求头
curl -I https://api.example.com

# 显示响应时间
curl -w "\nTime: %{time_total}s\n" https://api.example.com

# 生成curl命令
curl -v https://api.example.com 2>&1 | grep ">" | sed 's/> /curl -H "/'

性能分析

# 显示完整的时间信息
curl -w "\
    时间统计:\n\
    DNS解析: %{time_namelookup}s\n\
    TCP连接: %{time_connect}s\n\
    SSL握手: %{time_appconnect}s\n\
    等待响应: %{time_pretransfer}s\n\
    开始传输: %{time_starttransfer}s\n\
    总时间: %{time_total}s\n" \
    https://example.com

# 测试网站响应时间
curl -o /dev/null -s -w "%{time_total}\n" https://example.com

错误处理

# 失败时显示错误信息
curl -f https://api.example.com

# 重试失败的请求
curl --retry 3 https://api.example.com

# 设置重试间隔
curl --retry 3 --retry-delay 5 https://api.example.com

# 保存错误日志
curl -v https://api.example.com 2>error.log