curl命令文档

命令目录

curl

curl是一个利用URL规则在命令行下工作的文件传输工具,可以说是一款很强大的http命令行工具。它支持文件的上传和下载,是综合传输工具,但按传统,习惯称url为下载工具

#  -o 把url的内容输出到指定文件,而不是标准输出(控制台)
#  把阿里yum源下载下来,输出到文件 /etc/yum.repos.d/CentOS-Base.repo 中
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
# 如果链接有重定向时,可以使用 -L 参数跟踪下载
curl -L https://github.com/kubesphere/ks-installer/releases/download/v3.3.2/cluster-configuration.yaml -o cluster-configuration.yaml

查看帮助文档

curl --manual
curl --manual |grep '\-write\-out' -A 10

查看接口的响应时间

curl -o /dev/null -s -w "Total time: %{time_total}s\n"  https://www.baidu.com
# -o 丢弃响应内容(不显示)
# -s 静默模式(不显示进度/错误信息)
# -w 定义输出格式
curl -o /dev/null -s -w "
    DNS 解析时间: %{time_namelookup}s
    TCP 连接建立耗时(从开始到 TCP 连接完成的时间,包括 DNS 解析): %{time_connect}s
    SSL/TLS 握手耗时(仅 HTTPS,从开始到 SSL 握手完成的时间): %{time_appconnect}s
    请求准备耗时(从开始到请求即将发送的时间,包括 DNS + TCP + SSL): %{time_pretransfer}s
    重定向时间: %{time_redirect}s
    首字节耗时(TTFB,从开始到收到第一个字节的时间) (TTFB): %{time_starttransfer}s
    --------------------------
    读取响应结果时间=总时间-TTFB时间
    总时间: %{time_total}s\n" \
    https://baidu.com

常用参数解释

-v(--verbose)
#作用:显示详细的请求和响应信息(包括请求头、响应头、SSL 握手过程等)。适用场景:调试请求,尤其是排查连接失败、SSL 证书问题或 Headers 问题
-i(--include)
# 作用:在输出中包含 HTTP 响应头(Response Headers)。适用场景:查看服务器返回的状态码、Headers(如 Content-Type、Set-Cookie 等)。
-k(--insecure)
# 作用:跳过 SSL 证书验证(允许连接使用自签名或过期证书的 HTTPS 服务)。适用场景:测试内部服务、开发环境或绕过证书错误(生产环境不推荐)。
-I (--head) 等价于 -X HEAD
# 仅查看响应头(不显示响应体)
-X(--request)
# 作用:指定 HTTP 请求方法(如 GET、POST、PUT 等)。
-H(--header)
# 作用:添加请求头。 curl -H "Content-Type: application/json" -H "Authorization: Bearer token123" https://api.example.com
-d(--data)
# 作用:发送 POST 请求体数据(默认 Content-Type: application/x-www-form-urlencoded)。
# curl -d '{"key":"value"}' -H "Content-Type: application/json" https://api.example.com
-o(--output)
# 作用:将响应保存到文件。  curl -o output.html https://example.com
-O(--remote-name)
# 作用:下载文件并使用远程文件名保存   curl -O https://example.com/file.zip
-s (--silent)
# 静默模式(不显示进度/错误信息)
-x(--proxy)
# 作用:通过代理服务器发送请求。   curl -x http://proxy.example.com:8080 https://target.example.com
-m(--max-time)
# 设置请求超时时间(秒)。 curl -m 10 https://example.com  # 10秒超时
--retry
# 请求失败时重试次数。 curl --retry 3 https://example.com

认证

-u(--user)
# 作用:指定用户名和密码(Basic Auth) curl -u username:password https://api.example.com
--cookie
# 作用:发送 Cookie curl --cookie "sessionid=123abc" https://example.com
-c(--cookie-jar)
# 作用:将服务器返回的 Cookie 保存到文件 curl -c cookies.txt https://example.com

调试与高级参数

-L(--location)
# 作用:自动跟随重定向(3xx 响应) curl -L https://example.com/redirect
--resolve
# 作用:手动指定域名解析(绕过 DNS) curl --resolve example.com:443:93.184.216.34 https://example.com
--limit-rate
# 作用:限制下载/上传速度  curl --limit-rate 100K -O https://example.com/largefile.zip
posted @ 2024-11-04 23:18  菜阿  阅读(79)  评论(0)    收藏  举报