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

tablecell · August 12, 2020 · Last by serco replied at August 17, 2020 · 1454 hits
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.

You need to Sign in before reply, if you don't have an account, please Sign up first.