Linux下curl命令常用参数的详细说明

以下是对Linux下curl命令常用参数的详细说明,结合多个来源整理而成:


一、数据输出控制

  1. ​**-o  / --output**​
    将响应内容保存到指定文件。示例:

    curl -o output.html https://example.com  # 将网页保存为output.html
  2. ​**-O / --remote-name**​
    根据URL中的文件名自动保存文件。示例:

    curl -O https://example.com/file.zip  # 下载并保存为file.zip
  3. ​**-s / --silent**​
    静默模式,隐藏进度条和错误信息。适合脚本自动化场景:

    curl -s https://api.example.com/data  # 仅输出结果
  4. ​**-v / --verbose**​
    显示详细通信过程(请求头、响应头等),用于调试:

    curl -v https://example.com  # 显示完整HTTP交互

二、请求配置

  1. ​**-X  / --request**​
    指定HTTP方法(如GET、POST、PUT、DELETE)。示例:

    curl -X DELETE https://api.example.com/resource  # 发送DELETE请求
  2. ​**-H "Header: Value" / --header**​
    添加自定义请求头。常用于设置内容类型或认证令牌:

    curl -H "Content-Type: application/json" https://api.example.com
  3. ​**-d  / --data**​
    发送POST请求的表单数据或JSON数据。示例:

    curl -d "name=John&age=30" https://example.com/form  # 表单提交
    curl -d '{"key":"value"}' -H "Content-Type: application/json" https://api.example.com  # JSON数据
  4. ​**-F  / --form**​
    上传文件或表单数据(支持多部分编码)。示例:

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

三、认证与安全

  1. ​**-u  / --user**​
    指定用户名和密码进行HTTP基本认证。示例:

    curl -u admin:password https://protected.example.com
  2. ​**-k / --insecure**​
    跳过SSL证书验证(仅限测试环境使用):

    curl -k https://self-signed.example.com  # 忽略证书错误
  3. ​**--cert **​
    使用客户端证书进行HTTPS双向认证:

    curl --cert client.pem https://secure.example.com

四、高级功能

  1. ​**-L / --location**​
    自动跟随重定向(处理3xx状态码):

    curl -L http://short.url/redirect  # 跟踪最终地址
  2. ​**-C - / --continue-at**​
    断点续传(从上次中断位置继续下载):

    curl -C - -O https://example.com/largefile.zip
  3. ​**-A  / --user-agent**​
    伪装浏览器User-Agent:

    curl -A "Mozilla/5.0" https://example.com  # 模拟Chrome访问
  4. ​**-b  / --cookie**​
    发送Cookie或从文件读取Cookie:

    curl -b "session=1234" https://example.com  # 携带Cookie

五、其他实用参数

  • ​**-I / --head**​
    仅获取响应头信息(类似HTTP HEAD请求):

    curl -I https://example.com  # 检查服务器状态
  • ​**-x  / --proxy**​
    通过代理服务器访问:

    curl -x http://proxy:8080 https://example.com
  • ​**--limit-rate **​
    限制传输速率(如限制为100KB/s):

    curl --limit-rate 100K -O https://example.com/largefile.iso

完整参数列表

可通过curl --help查看所有参数,或访问官方文档获取更详细信息。

你可能感兴趣的:(服务器,Linux)