httpoison icon indicating copy to clipboard operation
httpoison copied to clipboard

HTTPoison.MaybeRedirect for 308 status_code ?

Open fchabouis opened this issue 3 years ago • 2 comments

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 !

fchabouis avatar Sep 21 '21 08:09 fchabouis

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

AntoineAugusti avatar Oct 11 '21 08:10 AntoineAugusti

Thanks, @AntoineAugusti !

edgurgel avatar Oct 14 '21 20:10 edgurgel

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

thbar avatar Apr 24 '23 08:04 thbar

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

thbar avatar Apr 24 '23 08:04 thbar

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

thbar avatar Jul 25 '23 12:07 thbar

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

AntoineAugusti avatar Oct 25 '23 14:10 AntoineAugusti

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

starcraft66 avatar Nov 05 '23 23:11 starcraft66

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.

edgurgel avatar Nov 07 '23 07:11 edgurgel

Tried today with

  • httpoison 2.1.0
  • hackney 1.20.1

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: []
  }
}

AntoineAugusti avatar Nov 15 '23 08:11 AntoineAugusti

 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)

edgurgel avatar Nov 28 '23 05:11 edgurgel

Oh, my bad @edgurgel 🙈😬

Can confirm it works fine, thanks!

AntoineAugusti avatar Nov 28 '23 09:11 AntoineAugusti