redirects not followed when streaming the request body
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)
You should go next to actually redirect the reauest. Redirection should probably handled before though. It will be fixed in the next release.
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:
- Keep the current way, ie simply return the status.
- 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}.
- 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?
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
why did you close the issue?
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.
This is expected behavior and not a bug to fix.
When using streaming body mode (body = stream), hackney cannot automatically follow redirects because:
- The request body has already been sent to the server
- The body is streamed incrementally and not kept in memory
- There's no way to "replay" the stream for the redirect
When you stream a request body and receive a redirect response:
- The
follow_redirectoption 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:
- Don't stream the body (provide the full body upfront)
- Handle redirects manually when streaming
Closing as expected behavior. The documentation improvement has been noted in the labels.