edgurgel / httpoison

Yet Another HTTP client for Elixir powered by hackney
https://hex.pm/packages/httpoison
MIT License
2.23k stars 339 forks source link

Response #444

Closed navyad closed 3 years ago

navyad commented 3 years ago

There is a public API that perfectly works in the browser and returns the JSON response but using the latest version of Httpoision It does not work as expected:

HTTP (GET) : 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
 }}

Also to point out the status_code is coming from the response is 301. 301 is for a resource that is permanently removed, but it seems that is not the case here.

sreecodeslayer commented 3 years ago

This is expected. You need to handle follow redirect for the 301 using the {"Location", "https://yts.mx/api/v2/list_movies.json?query_term=tt11296058"} response header. Browser does this for you

navyad commented 3 years ago

@sreecodeslayer Thanks, It worked.

HTTPoison.get(url, [], follow_redirect: true)