插件窝 干货文章 linux curl命令参数有哪些及怎么使用

linux curl命令参数有哪些及怎么使用

example curl HTTPS bash 178    来源:    2025-04-27

Linux curl 命令参数详解及使用指南

curl 是一个功能强大的命令行工具,用于传输数据,支持多种协议(HTTP、HTTPS、FTP、FTPS、SCP、SFTP等)。以下是 curl 的主要参数和使用方法。

基本语法

curl [options] [URL...]

常用参数分类

请求相关

  • -X--request: 指定 HTTP 请求方法 (GET, POST, PUT, DELETE 等)

    curl -X POST https://example.com/api
    
  • -H--header: 添加 HTTP 请求头

    curl -H "Content-Type: application/json" https://example.com
    
  • -d--data: 发送 POST 请求的数据

    curl -d "name=John&age=30" https://example.com
    
  • --data-binary: 发送二进制数据

    curl --data-binary @file.txt https://example.com
    
  • --data-urlencode: URL 编码发送的数据

    curl --data-urlencode "name=John Doe" https://example.com
    
  • -G--get: 将 -d 数据作为 URL 查询参数发送 (GET 请求)

    curl -G -d "q=keyword" https://example.com/search
    

输出控制

  • -o--output: 将输出写入文件

    curl -o output.html https://example.com
    
  • -O--remote-name: 使用远程文件名保存

    curl -O https://example.com/file.zip
    
  • -s--silent: 静默模式 (不显示进度和错误信息)

    curl -s https://example.com
    
  • -v--verbose: 显示详细操作信息

    curl -v https://example.com
    
  • --trace: 显示完整的跟踪信息

    curl --trace trace.txt https://example.com
    

认证相关

  • -u--user: 指定用户名和密码

    curl -u username:password https://example.com
    
  • --basic: 强制使用基本认证

    curl --basic -u username:password https://example.com
    
  • --digest: 使用摘要认证

    curl --digest -u username:password https://example.com
    

代理设置

  • -x--proxy: 使用代理服务器

    curl -x http://proxy.example.com:8080 https://example.com
    
  • --proxy-user: 代理认证

    curl --proxy-user user:pass -x http://proxy.example.com https://example.com
    

连接控制

  • --connect-timeout: 连接超时时间 (秒)

    curl --connect-timeout 30 https://example.com
    
  • -m--max-time: 最大传输时间 (秒)

    curl -m 60 https://example.com
    
  • --retry: 失败重试次数

    curl --retry 3 https://example.com
    

文件传输

  • -T--upload-file: 上传文件

    curl -T localfile.txt ftp://example.com/
    
  • -C--continue-at: 断点续传

    curl -C - -O https://example.com/bigfile.zip
    

其他常用

  • -L--location: 跟随重定向

    curl -L https://example.com
    
  • -k--insecure: 允许不安全的 SSL 连接

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

    curl --cacert /path/to/cert.pem https://example.com
    
  • -I--head: 只获取 HTTP 头信息

    curl -I https://example.com
    
  • -A--user-agent: 设置 User-Agent

    curl -A "Mozilla/5.0" https://example.com
    
  • -e--referer: 设置 Referer

    curl -e "https://google.com" https://example.com
    

实用示例

  1. 发送 JSON 数据

    curl -H "Content-Type: application/json" -X POST -d '{"username":"xyz","password":"xyz"}' https://example.com/api
    
  2. 下载文件并显示进度

    curl -# -O https://example.com/file.zip
    
  3. 保存 cookies 并在后续请求中使用

    curl -c cookies.txt https://example.com/login
    curl -b cookies.txt https://example.com/dashboard
    
  4. 上传多个文件

    curl -F "file=@file1.txt" -F "file=@file2.txt" https://example.com/upload
    
  5. 测试 API 端点

    curl -X GET "https://api.example.com/users?id=123" -H "Authorization: Bearer token123"
    
  6. 限制下载速度

    curl --limit-rate 100K -O https://example.com/bigfile.iso
    
  7. 使用 HTTP/2

    curl --http2 https://example.com
    
  8. 获取 HTTP 头信息

    curl -I https://example.com
    

要查看 curl 的完整参数列表,可以使用 man curlcurl --help 命令。