HTTPoison 跟随重定向
HTTPoison follow redirects
有一个 public API 在浏览器中完美运行,returns JSON 响应但是使用最新版本的 Httpoision 它没有按预期工作:
HTTP(获取):https://yts.lt/api/v2/list_movies.json?query_term=tt11296058
{:ok,
%HTTPoison.Response{
body: "",
headers: [
{"Date", "Tue, 22 Jun 2021 11:42:20 GMT"},
{"Transfer-Encoding", "chunked"},
{"Connection", "keep-alive"},
{"Cache-Control", "max-age=3600"},
{"Expires", "Tue, 22 Jun 2021 12:42:20 GMT"},
{"Location",
"https://yts.mx/api/v2/list_movies.json?query_term=tt11296058"},
{"cf-request-id", "0ad5205cb800004da508b04000000001"},
{"Expect-CT",
"max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\""},
{"Report-To",
"{\"endpoints\":[{\"url\":\"https:\/\/a.nel.cloudflare.com\/report\/v2?s=O80%2B5KfZ6d3G3Fz0NBGlep%2BetzQAvaUDIvVW09DUB2QMtJpd1XxupK621LhGR8EqiOsOY%2B55BdaHAljyLCEumHyb0rHSqk526jMQ5NxuLUi%2FVdbX\"}],\"group\":\"cf-nel\",\"max_age\":604800}"},
{"NEL", "{\"report_to\":\"cf-nel\",\"max_age\":604800}"},
{"Server", "cloudflare"},
{"CF-RAY", "663536745c654da5-BOM"},
{"alt-svc",
"h3-27=\":443\"; ma=86400, h3-28=\":443\"; ma=86400, h3-29=\":443\"; ma=86400, h3=\":443\"; ma=86400"}
],
request: %HTTPoison.Request{
body: "",
headers: [],
method: :get,
options: [],
params: %{},
url: "https://yts.lt/api/v2/list_movies.json?query_term=tt11296058"
},
request_url: "https://yts.lt/api/v2/list_movies.json?query_term=tt11296058",
status_code: 301
}}
还要指出 status_code 来自响应 301。
301 是针对永久删除的资源,但这里似乎不是这种情况。
项目依赖项:
Elixir: 1.12.1
OTP: 24
{:httpoison, "~> 1.8"},
{:floki, "~> 0.20.4"},
{:poison, "~> 3.1"},
{:hackney, github: "benoitc/hackney", override: true},
301 is for a resource that is permanently removed
绝对不是。这是 redirection(“永久移动”的代码。)
浏览器显然会自行处理重定向,代码必须自行处理。收到 301
后,您的代码应在指定位置重复请求。在您提供的回复中,回复中没有指定位置,这很奇怪,但那是另一个问题。
HTTPoison
可以是 instructed to handle redirects,不过。
您需要传递 follow_redirect
选项:
HTTPoison.get(url, [], follow_redirect: true)
示例:
iex(1)> Mix.install [:httpoison]
:ok
iex(2)> url = "https://yts.lt/api/v2/list_movies.json?query_term=tt11296058"
"https://yts.lt/api/v2/list_movies.json?query_term=tt11296058"
iex(3)> HTTPoison.get(url, [], follow_redirect: true)
{:ok,
%HTTPoison.Response{
body: "{\"status\":\"ok\",\"status_message\":\"Query was successful\",\"data\":
...
有一个 public API 在浏览器中完美运行,returns JSON 响应但是使用最新版本的 Httpoision 它没有按预期工作:
HTTP(获取):https://yts.lt/api/v2/list_movies.json?query_term=tt11296058
{:ok,
%HTTPoison.Response{
body: "",
headers: [
{"Date", "Tue, 22 Jun 2021 11:42:20 GMT"},
{"Transfer-Encoding", "chunked"},
{"Connection", "keep-alive"},
{"Cache-Control", "max-age=3600"},
{"Expires", "Tue, 22 Jun 2021 12:42:20 GMT"},
{"Location",
"https://yts.mx/api/v2/list_movies.json?query_term=tt11296058"},
{"cf-request-id", "0ad5205cb800004da508b04000000001"},
{"Expect-CT",
"max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\""},
{"Report-To",
"{\"endpoints\":[{\"url\":\"https:\/\/a.nel.cloudflare.com\/report\/v2?s=O80%2B5KfZ6d3G3Fz0NBGlep%2BetzQAvaUDIvVW09DUB2QMtJpd1XxupK621LhGR8EqiOsOY%2B55BdaHAljyLCEumHyb0rHSqk526jMQ5NxuLUi%2FVdbX\"}],\"group\":\"cf-nel\",\"max_age\":604800}"},
{"NEL", "{\"report_to\":\"cf-nel\",\"max_age\":604800}"},
{"Server", "cloudflare"},
{"CF-RAY", "663536745c654da5-BOM"},
{"alt-svc",
"h3-27=\":443\"; ma=86400, h3-28=\":443\"; ma=86400, h3-29=\":443\"; ma=86400, h3=\":443\"; ma=86400"}
],
request: %HTTPoison.Request{
body: "",
headers: [],
method: :get,
options: [],
params: %{},
url: "https://yts.lt/api/v2/list_movies.json?query_term=tt11296058"
},
request_url: "https://yts.lt/api/v2/list_movies.json?query_term=tt11296058",
status_code: 301
}}
还要指出 status_code 来自响应 301。 301 是针对永久删除的资源,但这里似乎不是这种情况。
项目依赖项:
Elixir: 1.12.1
OTP: 24
{:httpoison, "~> 1.8"},
{:floki, "~> 0.20.4"},
{:poison, "~> 3.1"},
{:hackney, github: "benoitc/hackney", override: true},
301 is for a resource that is permanently removed
绝对不是。这是 redirection(“永久移动”的代码。)
浏览器显然会自行处理重定向,代码必须自行处理。收到 301
后,您的代码应在指定位置重复请求。在您提供的回复中,回复中没有指定位置,这很奇怪,但那是另一个问题。
HTTPoison
可以是 instructed to handle redirects,不过。
您需要传递 follow_redirect
选项:
HTTPoison.get(url, [], follow_redirect: true)
示例:
iex(1)> Mix.install [:httpoison]
:ok
iex(2)> url = "https://yts.lt/api/v2/list_movies.json?query_term=tt11296058"
"https://yts.lt/api/v2/list_movies.json?query_term=tt11296058"
iex(3)> HTTPoison.get(url, [], follow_redirect: true)
{:ok,
%HTTPoison.Response{
body: "{\"status\":\"ok\",\"status_message\":\"Query was successful\",\"data\":
...