utf-8 - httpoison - 响应正文显示乱码文本而不是 html

标签 utf-8 elixir httpoison

如果我尝试:

url = "https://www.economist.com/news/finance-and-economics/21727073-economists-struggle-work-out-how-much-free-economy-comes-cost"    
{:ok, %HTTPoison.Response{status_code: 200, body: body}} = HTTPoison.get(url)
IO.binwrite body

我在控制台中看到乱码文本(而不是 html)。但是如果我在网页上查看源代码,我会在那里看到 html。我做错了什么?

PS:它与 js http 客户端(axios.js)一起工作正常,不知道为什么它不能与 httpoison 一起工作

最佳答案

该 URL 以 gzip 形式返回正文,并通过发送 header Content-Encoding: gzip 来指示这一点。 hackney(HTTPoison 所基于的库)不会自动对其进行解码。此功能will likely be added at some point 。在此之前,如果 Content-Encodinggzip,您可以使用 :zlib 模块自行解码正文:

url = "https://www.economist.com/news/finance-and-economics/21727073-economists-struggle-work-out-how-much-free-economy-comes-cost"

{:ok, %HTTPoison.Response{status_code: 200, headers: headers, body: body}} = HTTPoison.get(url)

gzip? = Enum.any?(headers, fn {name, value} ->
  # Headers are case-insensitive so we compare their lower case form.
  :hackney_bstr.to_lower(name) == "content-encoding" &&
    :hackney_bstr.to_lower(value) == "gzip"
end)

body = if gzip?, do: :zlib.gunzip(body), else: body

IO.write body

关于utf-8 - httpoison - 响应正文显示乱码文本而不是 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45908080/

相关文章:

ruby-on-rails - 西里尔字符串 Я̆ Я̄ Я̈ 在 ruby​​ 和其他编程语言中返回长度 2 而不是 1

soap - 使用 Erlang 洗涤剂库在 SOAP 调用中传递 oauth header

Java 将 UTF-8 文本文件转换为 Cp1250

mysql - 导出数据库时编码丢失

python - 如何用python解码代表utf-8的字符串?

elixir - 如何保存 IEx 历史记录?

regex - 在 Elixir 中,如何用非单词字符作为分隔符来分割字符串,同时还允许使用 +、- 等数学运算符?

elixir - Phoenix 混合任务上的 HTTPoison ArgumentError

elixir - HTTPoison.request!/5 以奇怪的原因引发 %HTTPoison.Error

networking - 在定义的接口(interface) IP 地址上启动 erlang/elixir