curl命令导致你下载的文件为空原因分析

文章目录

    • 1.前言
    • 2. 通过curl -O 下载远端文件
      • 2.1 执行curl -O下载远端文件
      • 2.2 通过curl -v 查看详细的请求和响应的信息
    • 3.通过在curl -O 中增加 -L 参数保证curl能够自动跟踪和请求远端返回的重定向地址
    • 4.结论


1.前言

最近在进行线上项目调试的过程中需要安装调试工具,因为没有安装wget命令,只安装了curl工具, 所以尝试使用curl -O 命令下载调试工具,结果发现下载的文件为空,通过查看官方手册,了解到curl 默认是不追踪重定向地址的,需要添加 -L 参数才能达到自动跟踪重定向地址的目的,本文 将具体的排查和解决问题的步骤整理成此篇文章分享给大家。

在这里插入图片描述

2. 通过curl -O 下载远端文件

2.1 执行curl -O下载远端文件

预期是能正常将jaatch包下载至当前目录,但执行结果是下载文件为空

执行命令如下:

curl -O  https://github.com/jattach/jattach/releases/download/v2.1/jattach

下载结果为空文件
在这里插入图片描述

2.2 通过curl -v 查看详细的请求和响应的信息

curl -O -v https://github.com/jattach/jattach/releases/download/v2.1/jattach

可以看到远端有一个302重定向的响应,curl没有重新请求这个重定向的地址
在这里插入图片描述
详细请求、响应信息如下

# curl -O -v https://github.com/jattach/jattach/releases/download/v2.1/jattach% Total    % Received % Xferd  Average Speed   Time    Time     Time  CurrentDload  Upload   Total   Spent    Left  Speed0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying 20.205.243.166:443...
* Connected to github.com (20.205.243.166) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*  CAfile: /etc/ssl/certs/ca-certificates.crt
*  CApath: /etc/ssl/certs
} [5 bytes data]
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
} [512 bytes data]
* TLSv1.3 (IN), TLS handshake, Server hello (2):
{ [122 bytes data]
* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
{ [19 bytes data]
* TLSv1.3 (IN), TLS handshake, Certificate (11):
{ [2459 bytes data]
* TLSv1.3 (IN), TLS handshake, CERT verify (15):
{ [79 bytes data]
* TLSv1.3 (IN), TLS handshake, Finished (20):
{ [36 bytes data]
* TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1):
} [1 bytes data]
* TLSv1.3 (OUT), TLS handshake, Finished (20):
} [36 bytes data]
* SSL connection using TLSv1.3 / TLS_AES_128_GCM_SHA256
* ALPN, server accepted to use h2
* Server certificate:
*  subject: C=US; ST=California; L=San Francisco; O=GitHub, Inc.; CN=github.com
*  start date: Feb 14 00:00:00 2023 GMT
*  expire date: Mar 14 23:59:59 2024 GMT
*  subjectAltName: host "github.com" matched cert's "github.com"
*  issuer: C=US; O=DigiCert Inc; CN=DigiCert TLS Hybrid ECC SHA384 2020 CA1
*  SSL certificate verify ok.
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
} [5 bytes data]
* Using Stream ID: 1 (easy handle 0x55cc914ed560)
} [5 bytes data]
> GET /jattach/jattach/releases/download/v2.1/jattach HTTP/2
> Host: github.com
> user-agent: curl/7.74.0
> accept: */*
>
{ [5 bytes data]
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
{ [57 bytes data]
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
{ [57 bytes data]
* old SSL session ID is stale, removing
{ [5 bytes data]
* Connection state changed (MAX_CONCURRENT_STREAMS == 100)!
} [5 bytes data]
< HTTP/2 302
< server: GitHub.com
< date: Wed, 13 Dec 2023 01:36:02 GMT
< content-type: text/html; charset=utf-8
< vary: X-PJAX, X-PJAX-Container, Turbo-Visit, Turbo-Frame, Accept-Encoding, Accept, X-Requested-With
< location: https://objects.githubusercontent.com/github-production-release-asset-2e65be/54976015/b8f92250-3270-42cb-bf73-004a9eb4233e?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20231213%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20231213T013602Z&X-Amz-Expires=300&X-Amz-Signature=35bc8eaa900ac77f4875fbfc757592331025692f19d543e0e69f3c4c3342c530&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=54976015&response-content-disposition=attachment%3B%20filename%3Djattach&response-content-type=application%2Foctet-stream
< cache-control: no-cache
< strict-transport-security: max-age=31536000; includeSubdomains; preload
< x-frame-options: deny
< x-content-type-options: nosniff
< x-xss-protection: 0
< referrer-policy: no-referrer-when-downgrade
< content-security-policy: default-src 'none'; base-uri 'self'; child-src github.com/assets-cdn/worker/gist.github.com/assets-cdn/worker/; connect-src 'self' uploads.github.com www.githubstatus.com collector.github.com raw.githubusercontent.com api.github.com github-cloud.s3.amazonaws.com github-production-repository-file-5c1aeb.s3.amazonaws.com github-production-upload-manifest-file-7fdce7.s3.amazonaws.com github-production-user-asset-6210df.s3.amazonaws.com cdn.optimizely.com logx.optimizely.com/v1/events api.githubcopilot.com objects-origin.githubusercontent.com *.actions.githubusercontent.com wss://*.actions.githubusercontent.com productionresultssa0.blob.core.windows.net/ productionresultssa1.blob.core.windows.net/ productionresultssa2.blob.core.windows.net/ productionresultssa3.blob.core.windows.net/ productionresultssa4.blob.core.windows.net/ productionresultssa5.blob.core.windows.net/ productionresultssa6.blob.core.windows.net/ productionresultssa7.blob.core.windows.net/ productionresultssa8.blob.core.windows.net/ productionresultssa9.blob.core.windows.net/ github-production-repository-image-32fea6.s3.amazonaws.com github-production-release-asset-2e65be.s3.amazonaws.com insights.github.com wss://alive.github.com; font-src github.githubassets.com; form-action 'self' github.com gist.github.com objects-origin.githubusercontent.com; frame-ancestors 'none'; frame-src viewscreen.githubusercontent.com notebooks.githubusercontent.com support.github.com; img-src 'self' data: github.githubassets.com media.githubusercontent.com camo.githubusercontent.com identicons.github.com avatars.githubusercontent.com github-cloud.s3.amazonaws.com objects.githubusercontent.com secured-user-images.githubusercontent.com/ user-images.githubusercontent.com/ private-user-images.githubusercontent.com opengraph.githubassets.com github-production-user-asset-6210df.s3.amazonaws.com customer-stories-feed.github.com spotlights-feed.github.com objects-origin.githubusercontent.com *.githubusercontent.com; manifest-src 'self'; media-src github.com user-images.githubusercontent.com/ secured-user-images.githubusercontent.com/ private-user-images.githubusercontent.com github-production-user-asset-6210df.s3.amazonaws.com gist.github.com; script-src github.githubassets.com; style-src 'unsafe-inline' github.githubassets.com; upgrade-insecure-requests; worker-src github.com/assets-cdn/worker/ gist.github.com/assets-cdn/worker/
< content-length: 0
< x-github-request-id: 6F1A:13E145:136CA4:156175:65790AA0
<
{ [0 bytes data]0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
* Connection #0 to host github.com left intact

3.通过在curl -O 中增加 -L 参数保证curl能够自动跟踪和请求远端返回的重定向地址

curl命令默认是不跟踪重定向地址的,通过增加 -L 参数可以保证自动跟踪重定向地址,默认可以跟踪次数为50次 如果想调整重定向跟踪次数可以通过–max-redirs参数指定

在这里插入图片描述
如下展示了增加 -L 参数后,成功下载了jattach文件

# curl -O -L https://github.com/jattach/jattach/releases/download/v2.1/jattach
% Total    % Received % Xferd  Average Speed   Time    Time     Time  CurrentDload  Upload   Total   Spent    Left  Speed
0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100 24850  100 24850    0     0     95      0  0:04:21  0:04:19  0:00:02  6454

查看下载的文件如下
在这里插入图片描述

4.结论

在实际项目中经常遇到接口响应超时、报错等异常场景,curl命令可以很方便的帮助我们进行接口的定位,可以说是广大 程序员的比较趁手的小工具,但是curl命令默认不会跟踪重定向地址,若想要curl跟踪重定向地址需要使用 -L 参数, 默认使用 -L时最大的跟踪次数为50次,正常情况下是足够用的了,如果想单独指定跟踪次数,可以通过使用 --max-redirs 指定。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/427223.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

活动回顾丨云原生技术实践营上海站「云原生 AI 大数据」专场(附 PPT)

AI 势不可挡&#xff0c;“智算”赋能未来。2024 年 1 月 5 日&#xff0c;云原生技术实践营「云原生 AI &大数据」专场在上海落幕。活动聚焦容器、可观测、微服务产品技术领域&#xff0c;以云原生 AI 工程化落地为主要方向&#xff0c;希望帮助企业和开发者更快、更高效地…

版图设计工程师的面试一般会问啥?

之前全面为大家解析了模拟版图&#xff0c;但面对面对即将找工作或者是面对明年春招的同学&#xff0c;可能对于模拟版图面试这块更感兴趣。 秋招已经进入白热化阶段&#xff0c;今天移知教育为大家整理出&#xff0c;模拟版图几道模拟版图面试题&#xff0c;带你直击模拟版图…

蓝牙BLE基础知识

目录 一、初识蓝牙BLE 1.课程介绍 2.为什么需要蓝牙技术 3.蓝牙发展历史 4.蓝牙技术优势 5.蓝牙技术简介 6.学习补充 二、物理层&#xff08;Physical layer&#xff09; 1.模拟调制 2.数字调制 3.射频信道 4.学习补充 三、链路层&#xff08;link layer&#xff0…

基于若依的ruoyi-nbcio流程管理系统一种简单的动态表单模拟测试实现(五)

更多ruoyi-nbcio功能请看演示系统 gitee源代码地址 前后端代码&#xff1a; https://gitee.com/nbacheng/ruoyi-nbcio 演示地址&#xff1a;RuoYi-Nbcio后台管理系统 更多nbcio-boot功能请看演示系统 gitee源代码地址 后端代码&#xff1a; https://gitee.com/nbacheng/n…

Linux 一键部署influxd2-telegraf

influxd2前言 influxd2 是 InfluxDB 2.x 版本的后台进程,是一个开源的时序数据库平台,用于存储、查询和可视化时间序列数据。它提供了一个强大的查询语言和 API,可以快速而轻松地处理大量的高性能时序数据。 telegraf 是一个开源的代理程序,它可以收集、处理和传输各种不…

多维时序 | Matlab实现WOA-TCN-Multihead-Attention鲸鱼算法优化时间卷积网络结合多头注意力机制多变量时间序列预测

多维时序 | Matlab实现WOA-TCN-Multihead-Attention鲸鱼算法优化时间卷积网络结合多头注意力机制多变量时间序列预测 目录 多维时序 | Matlab实现WOA-TCN-Multihead-Attention鲸鱼算法优化时间卷积网络结合多头注意力机制多变量时间序列预测效果一览基本介绍程序设计参考资料 效…

VUE3好看的我的家乡网站模板源码

文章目录 1.设计来源1.1 首页界面1.2 旅游导航界面1.3 上海景点界面1.4 上海美食界面1.5 上海故事界面1.6 联系我们界面1.7 在线留言界面 2.效果和结构2.1 动态效果2.2 代码结构 源码下载 作者&#xff1a;xcLeigh 文章地址&#xff1a;https://blog.csdn.net/weixin_43151418/…

Docker容器引擎(1)

目录 一.Docker 概述 为什么要用到容器&#xff1f; docker是什么&#xff1f; 容器与虚拟机的区别&#xff1f; docker的三个核心概念&#xff1a; 二.安装docker 安装依赖包&#xff1a; 安装 Docker-CE并设置为开机自动启动&#xff1a; 查看 docker 版本信息&#…

java程序判等问题

注意 equals 和 的区别 对基本类型&#xff0c;比如 int、long&#xff0c;进行判等&#xff0c;只能使用 &#xff0c;比较的是直接值。因为基本类型的值就是其数值。对引用类型&#xff0c;比如 Integer、Long 和 String&#xff0c;进行判等&#xff0c;需要使用 equals 进…

STL第一讲

一、认识headers、版本、重要资源 1. C Standard Library和Standard Template Library 前者&#xff1a;c标准库&#xff1b;后者直译为“标准模板库” 区别: C标准库&#xff1a;是c编译器提供的自带的头文件(不带.h后缀)新版兼容C的头文件的形式cxxxx&#xff1b;旧版的xxx…

Mybatis----缓存

MyBatis是一个流行的Java持久化框架&#xff0c;它提供了一个灵活的缓存机制来提高查询性能。 MyBatis的缓存机制主要分为一级缓存和二级缓存。 一级缓存是指在同一个SqlSession中&#xff0c;查询结果会被缓存起来&#xff0c;当再次执行同样的查询时&#xff0c;直接从缓存中…