ruby - 检查网络文件是否存在,而不下载它?

标签 ruby

是否可以在不实际下载文件的情况下检查文件是否存在?

我有这么大的 (~ 40mb) 文件,例如:

http://mirrors.sohu.com/mysql/MySQL-6.0/MySQL-6.0.11-0.glibc23.src.rpm

这与 ruby​​ 严格相关,但如果发件人可以设置内容长度就好了。

RestClient.get "http://mirrors.sohu.com/mysql/MySQL-6.0/MySQL-6.0.11-0.glibc23.src.rpm", 
               headers: {"Content-Length" => 100}

最佳答案

试试 RestClient.head。 (参见 https://www.rfc-editor.org/rfc/rfc9110.html#name-head)

The HTTP HEAD method is identical to GET except that the server MUST NOT send content in the response. HEAD is used to obtain metadata about the selected representation without transferring its representation data, often for the sake of testing hypertext links or finding recent modifications.

RFC 9110 HTTP Semantics

关于ruby - 检查网络文件是否存在,而不下载它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7655235/

相关文章:

ruby - 使用 Ruby 编写一个简单的 Windows 7 GUI 应用程序

使用 attr_accessor 和初始化方法的 Ruby 类

ruby - 以非 root 用户身份从 ruby​​ 在 Xvfb 中运行 Selenium::WebDriver::Firefox

ruby - 如何衡量点击率的统计显着性?

ruby - 可枚举模块如何通过在 Ruby 中的类上添加 each 方法来工作?

ruby-on-rails - 用整数写一个 Rails 验证器

javascript - rails/JSON : How to use JSON for jquery UI autocomplete form

ruby - 解释简洁的 ruby​​ 'nil' 错误

ruby - Rails Fixtures 中的引用用户

ruby-on-rails - 事件记录 : How to include another model based on Enum?