Linux curl命令详解使用

curl 是一个非常强大且灵活的工具,支持多种协议(如 HTTP、HTTPS、FTP 等),并通过各种选项支持不同的请求方式、认证机制、代理设置、传输限制等。这些参数可以极大地提高网络请求中的效率和灵活性。

curl 命令的基本语法

curl [options] [URL...]
  • options:指定不同操作的参数。
  • URL:目标资源的地址,可以是一个或多个。

curl 常用参数详解

1. 数据输出相关选项
  • -o [file]:将响应内容保存到指定文件。

    curl -o output.html https://example.com
    
  • -O:将文件下载并保留其原始文件名。

    curl -O https://example.com/file.zip
    
  • -J:根据 Content-Disposition 头部信息保存文件名(与 -O 配合使用)。

    curl -OJ https://example.com/download
    
  • -C -:断点续传。继续从上次中断的位置下载文件。

    curl -C - -O https://example.com/largefile.zip
    
  • -L:跟随重定向(3xx 状态码)。

    curl -L https://short.url/redirect
    
  • -s:静默模式,不输出错误和进度信息。

    curl -s https://example.com
    
  • -S:与 -s 一起使用,在静默模式下显示错误信息。

    curl -sS https://example.com
    
  • -v:详细模式,显示请求和响应的详细信息,用于调试。

    curl -v https://example.com
    
  • -i:显示响应头和响应体。

    curl -i https://example.com
    
  • -I:仅显示响应头。

    curl -I https://example.com
    
  • -k:忽略 SSL 证书验证错误。

    curl -k https://self-signed.badssl.com/
    
2. 数据发送相关选项
  • -d [data]:使用 POST 方法发送数据。数据可以是键值对或 JSON 格式。

    curl -d "key1=value1&key2=value2" https://api.example.com/submit
    curl -d '{"name":"John","age":30}' -H "Content-Type: application/json" https://api.example.com/submit
    
  • -F [name=content]:提交表单数据,可以上传文件。

    curl -F "file=@/path/to/file.jpg" https://api.example.com/upload
    
  • -X [method]:指定 HTTP 请求方法,如 GET、POST、PUT、DELETE 等。

    curl -X DELETE https://api.example.com/resource/1
    
3. 请求头相关选项
  • -H [header]:自定义 HTTP 请求头信息,如 Content-TypeAuthorization

    curl -H "Authorization: Bearer token123" https://api.example.com/data
    
  • -A [user-agent]:设置用户代理(User-Agent),模拟特定浏览器请求。

    curl -A "Mozilla/5.0" https://example.com
    
  • -e [referer]:设置 Referer 请求头,用于模拟从特定页面跳转的请求。

    curl -e https://example.com https://other.example.com
    
  • --compressed:请求压缩内容并自动解压。

    curl --compressed https://example.com
    
  • -b [cookie]:发送带有指定 Cookie 的请求。

    curl -b "name=value" https://example.com
    
  • -c [cookie-file]:将服务器返回的 Cookie 保存到指定文件中。

    curl -c cookies.txt https://example.com
    
4. 认证与安全相关选项
  • -u [user:password]:用于 HTTP 基本认证,发送用户名和密码。

    curl -u user:pass https://example.com/protected
    
  • --proxy-user [user:password]:在使用代理服务器时,指定代理的用户名和密码。

    curl --proxy-user proxyuser:password -x http://proxy.example.com:8080 https://example.com
    
  • --key [key-file]:使用指定的私钥文件(用于 HTTPS)。

    curl --key /path/to/private.key https://example.com
    
  • --cert [cert-file]:使用指定的客户端证书文件。

    curl --cert /path/to/cert.pem https://example.com
    
  • --cacert [CA-cert-file]:使用指定的 CA 证书文件验证服务器证书。

    curl --cacert /path/to/ca.crt https://example.com
    
  • --ssl-reqd:强制使用 SSL/TLS。

    curl --ssl-reqd https://example.com
    
5. 代理相关选项
  • -x [proxy]:使用指定的代理服务器发送请求。

    curl -x http://proxy.example.com:8080 https://example.com
    
  • --proxy-user [user:password]:在使用代理服务器时,指定代理的用户名和密码。

    curl --proxy-user proxyuser:password -x http://proxy.example.com:8080 https://example.com
    
  • --noproxy [no-proxy-list]:对于指定的主机,不使用代理。

    curl --noproxy "example.com" https://example.com
    
6. 限制与超时选项
  • --max-time [seconds]:设置最大请求时间,超过此时间 curl 会自动终止。

    curl --max-time 10 https://example.com
    
  • --connect-timeout [seconds]:设置最大连接时间。

    curl --connect-timeout 5 https://example.com
    
  • --limit-rate [speed]:限制传输速度,可以用 km 表示千字节/秒或兆字节/秒。

    curl --limit-rate 100k https://example.com
    
  • -m [seconds]:设置整个操作的超时时间,与 --max-time 类似。

    curl -m 20 https://example.com
    
7. 多文件与并行处理选项
  • -Z:启用并行处理多个 URL 请求。

    curl -Z https://example.com/page1 https://example.com/page2
    
  • -K [config-file]:从指定的配置文件中读取 curl 参数。

    curl -K config.txt
    
8. 调试与开发选项
  • --trace [file]:记录请求和响应的详细信息到文件。

    curl --trace trace.log https://example.com
    
  • --trace-ascii [file]:以 ASCII 格式记录 trace 信息。

    curl --trace-ascii trace.txt https://example.com
    
  • --stderr [file]:将错误信息重定向到指定文件。

    curl --stderr errors.log https://example.com
    
  • -w [format]:定义如何显示最终状态的格式,如 HTTP 状态码、传输时间等。

    curl -w "HTTP Code: %{http_code}\nTotal Time: %{time_total}\n" https://example.com
    
9. 其他选项
  • -q:禁用 .curlrc 配置文件。

    curl -q https://example.com
    
  • -h:显示帮助信息,列出所有可用选项。

    curl -h
    
  • -V:显示 curl 的版本信息。

    curl -V
    

`

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值