新手问题 Typhoeus 对 gzip 内容不解压吗?

tablecell · 2020年08月12日 · 最后由 serco 回复于 2020年08月17日 · 1451 次阅读
url='https://www.sohu.com/'
resp =Typhoeus.get(url,)
# resp=Typhoeus.get(url, headers: {"Accept-Encoding" => "gzip"})

puts resp.body.class
puts resp.body.encoding
puts resp.body.length

返回 string 长度是 0 用 verbose 是返回了 gzip 二进制数据 但是不显示

稍微看一下文档就有答案啊。 下面是 README 的原文,前一种用法才会解压,后一种不会。

Compression

Typhoeus.get("www.example.com", accept_encoding: "gzip")

The above has a different behavior than setting the header directly in the header hash, eg:

Typhoeus.get("www.example.com", headers: {"Accept-Encoding" => "gzip"})

Setting the header hash directly will not include the --compressed flag in the libcurl command and therefore libcurl will not decompress the response. If you want the --compressed flag to be added automatically, set :accept_encoding Typhoeus option.

需要 登录 后方可回复, 如果你还没有账号请 注册新账号