edgurgel / httpoison

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

HTTPoison.MaybeRedirect for 308 status_code ? #447

Closed fchabouis closed 1 year ago

fchabouis commented 3 years ago

Hi, I understand from the documentation that performing a get request on an URL responding with a 308 code should yield a HTTPoison.MaybeRedirect, allowing me to perform a redirect by hand easily.

https://hexdocs.pm/httpoison/HTTPoison.MaybeRedirect.html#t:t/0

But I get a classical %HTTPoison.Response instead.

For example :

iex(3)> HTTPoison.get("https://demo.data.gouv.fr/api/1/spatial/zones/fr:commune:44109@1943-01-01", [], [follow_redirect: true])
{:ok,
 %HTTPoison.Response{
   request_url: "https://demo.data.gouv.fr/api/1/spatial/zones/fr:commune:44109@1943-01-01",
   status_code: 308,
   body: "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">\n<title>Redirecting...</title>\n<h1>Redirecting...</h1>\n<p>You should be redirected automatically to target URL: <a href=\"https://demo.data.gouv.fr/api/1/spatial/zones/fr:commune:44109%401943-01-01/\">https://demo.data.gouv.fr/api/1/spatial/zones/fr:commune:44109%401943-01-01/</a>.  If not click the link.",
   headers: [
...

Am I misunderstanding the documentation ?

(Ideally, I'd like 308 response to be automatically followed, but it seems to come from the underlying hackney lib https://github.com/benoitc/hackney/issues/635)

Thanks !

AntoineAugusti commented 3 years ago

Opened a PR on the Hackney side https://github.com/benoitc/hackney/pull/692

edgurgel commented 3 years ago

Thanks, @AntoineAugusti !

thbar commented 1 year ago

Pending a release on Hackney side https://github.com/benoitc/hackney/pull/692#issuecomment-1468124743

thbar commented 1 year ago

(useful url for testing: https://httpbin.org/status/308)

thbar commented 1 year ago

Still not released (https://github.com/benoitc/hackney/compare/1.18.1...master), available in current master.

AntoineAugusti commented 1 year ago

Hackney released new versions recently, would it be possible to bump it?

starcraft66 commented 1 year ago

This should be fixed in https://github.com/edgurgel/httpoison/commit/5920e195437237f72673b0514656cedfea0b0603, would be nice to have a new release.

edgurgel commented 1 year ago

Hey team, HTTPoison declares hackney requirement as: [hackney](https://hex.pm/packages/hackney) ~> 1.17 so one can update hackney by doing mix deps.update hackney and that's it. No need to update HTTPoison unless hackney 2.0.0 comes out.

AntoineAugusti commented 1 year ago

Tried today with

and I get confusing results

iex> :hackney.request(:get, "https://www.data.gouv.fr/api/1/datasets/fichier-gtfs/", [], "", [{:follow_redirect, false}])
{:ok, 308,
 [
   {"server", "nginx"},
   {"date", "Wed, 15 Nov 2023 07:37:45 GMT"},
   {"content-type", "text/html; charset=utf-8"},
   {"content-length", "233"},
   {"location", "/api/1/datasets/ametis/"},
   {"access-control-allow-origin", "*"},
   {"pragma", "public"},
   {"cache-control", "public"},
   {"x-content-type-options", "nosniff"},
   {"x-xss-protection", "1; mode=block"},
   {"x-frame-options", "SAMEORIGIN"}
 ], #Reference<0.2597323666.388235265.31870>}
iex> :hackney.request(:get, "https://www.data.gouv.fr/api/1/datasets/fichier-gtfs/", [], "", [{:follow_redirect, true}]) 
{:ok, 200,
 [
   {"server", "nginx"},
   {"date", "Wed, 15 Nov 2023 07:37:49 GMT"},
   {"content-type", "application/json"},
   {"content-length", "8873"},
   {"vary", "Accept-Encoding"},
   {"access-control-allow-origin", "*"},
   {"pragma", "public"},
   {"cache-control", "public"},
   {"x-content-type-options", "nosniff"},
   {"x-xss-protection", "1; mode=block"},
   {"x-frame-options", "SAMEORIGIN"}
 ], #Reference<0.2597323666.387973123.235497>}
iex> HTTPoison.get!("https://www.data.gouv.fr/api/1/datasets/fichier-gtfs/", follow_redirect: true)
%HTTPoison.Response{
  status_code: 308,
  body: "<!doctype html>\n<html lang=en>\n<title>Redirecting...</title>\n<h1>Redirecting...</h1>\n<p>You should be redirected automatically to the target URL: <a href=\"/api/1/datasets/ametis/\">/api/1/datasets/ametis/</a>. If not, click the link.\n",
  headers: [
    {"server", "nginx"},
    {"date", "Wed, 15 Nov 2023 07:30:39 GMT"},
    {"content-type", "text/html; charset=utf-8"},
    {"content-length", "233"},
    {"location", "/api/1/datasets/ametis/"},
    {"access-control-allow-origin", "*"},
    {"pragma", "public"},
    {"cache-control", "public"},
    {"x-content-type-options", "nosniff"},
    {"x-xss-protection", "1; mode=block"},
    {"x-frame-options", "SAMEORIGIN"}
  ],
  request_url: "https://www.data.gouv.fr/api/1/datasets/fichier-gtfs/",
  request: %HTTPoison.Request{
    method: :get,
    url: "https://www.data.gouv.fr/api/1/datasets/fichier-gtfs/",
    headers: [follow_redirect: true],
    body: "",
    params: %{},
    options: []
  }
}
edgurgel commented 1 year ago
 HTTPoison.get!("https://www.data.gouv.fr/api/1/datasets/fichier-gtfs/", follow_redirect: true)

The options (follow_redirect: true) must be the last argument. The second argument is the list of headers.

HTTPoison.get!("https://www.data.gouv.fr/api/1/datasets/fichier-gtfs/", [], follow_redirect: true)
AntoineAugusti commented 12 months ago

Oh, my bad @edgurgel 🙈😬

Can confirm it works fine, thanks!