hackney icon indicating copy to clipboard operation
hackney copied to clipboard

redirects not followed when streaming the request body

Open jfis opened this issue 11 years ago • 6 comments

maybe this is intentional, or I'm missing something?

v 1.2.0 please forgive elixir syntax, not sure how to load hackney into erl

normal request returns redirect

iex(1)> :hackney.request(:get, "http://rdrct.herokuapp.com/1", [], "", [])
{:ok, 301,
 [{"Connection", "keep-alive"}, {"Server", "Cowboy"},
  {"Date", "Thu, 16 Jul 2015 23:48:48 GMT"}, {"Content-Length", "0"},
  {"Location", "/2"}, {"Via", "1.1 vegur"}], #Reference<0.0.0.3698>}

with follow redirect

iex(1)> :hackney.request(:get, "http://rdrct.herokuapp.com/1", [], "", [follow_redirect: true, max_redirect: 5])
{:ok, 200,
 [{"Connection", "keep-alive"}, {"Server", "Cowboy"},
  {"Date", "Thu, 16 Jul 2015 23:42:13 GMT"}, {"Content-Length", "2"},
  {"Via", "1.1 vegur"}], #Reference<0.0.0.3263>}

with stream request body

iex(1)> {:ok, client} = :hackney.request(:get, "http://rdrct.herokuapp.com/1", [], :stream, [follow_redirect: true, max_redirect: 5])
iex(2)> :hackney.start_response(client)
{:ok, 301,
 [{"Connection", "close"}, {"Server", "Cowboy"},
  {"Date", "Thu, 16 Jul 2015 23:51:47 GMT"}, {"Content-Length", "0"},
   {"Location", "/2"}, {"Via", "1.1 vegur"}], #Reference<0.0.0.3895>}

post

iex(3)> {:ok, client} = :hackney.request(:post, "http://rdrct.herokuapp.com/1", [], :stream, [force_redirect: true, follow_redirect: true, max_redirect: 5])
iex(4)> :hackney.send_body("test")
iex(5)> :hackney.start_response(client)
{:ok, 301,
[{"Connection", "close"}, {"Server", "Cowboy"},
 {"Date", "Fri, 17 Jul 2015 16:37:04 GMT"}, {"Content-Length", "0"},
 {"Location", "/2"}, {"Via", "1.1 vegur"}], #Reference<0.0.0.2196>}

(destination taken from https://github.com/benoitc/hackney/issues/170)

jfis avatar Jul 16 '15 23:07 jfis

You should go next to actually redirect the reauest. Redirection should probably handled before though. It will be fixed in the next release.

benoitc avatar Aug 01 '15 06:08 benoitc

Did some test. I was wrong. Since you are streaming the body, it's not kept around and there are no way yet to resend it. So the status is simply returned and you should handled the redirection by yourself.

At that point I am not sure what to do:

  1. Keep the current way, ie simply return the status.
  2. As above but to ease the work we may want to retrieve directly the body and release the socket directly. The response would be then {ok, 301, Headers, Body}.
  3. Log the request and replay the stream. The issue with that is that we would need to keep a log for all streamed requests which will impact the RAM or disk eventually.

Thoughts?

benoitc avatar Aug 21 '15 19:08 benoitc

i think it's probably best to keep it as is and not allow :stream with :follow_redirect, but it should be noted in the docs

jfis avatar Aug 21 '15 19:08 jfis

why did you close the issue?

benoitc avatar Jun 19 '20 07:06 benoitc

oh i'm sorry if i shouldn't have. i came across this on some feed and saw it was 5 years old and still open. i thought maybe you were waiting on me to close it or it was overlooked. i'll leave it be.

jfis avatar Jun 29 '20 01:06 jfis

This is expected behavior and not a bug to fix.

When using streaming body mode (body = stream), hackney cannot automatically follow redirects because:

  1. The request body has already been sent to the server
  2. The body is streamed incrementally and not kept in memory
  3. There's no way to "replay" the stream for the redirect

When you stream a request body and receive a redirect response:

  • The follow_redirect option is effectively ignored for this request
  • You receive the redirect status (301, 302, etc.) and headers
  • You should handle the redirect manually in your application

This is a limitation of streaming body mode that exists in most HTTP clients. If you need automatic redirect following, you have two options:

  1. Don't stream the body (provide the full body upfront)
  2. Handle redirects manually when streaming

Closing as expected behavior. The documentation improvement has been noted in the labels.

benoitc avatar Jan 20 '26 16:01 benoitc