Page MenuHomePhorge

No OneTemporary

Size
1 KB
Referenced Files
None
Subscribers
None
diff --git a/lib/tesla/middleware/follow_redirects.ex b/lib/tesla/middleware/follow_redirects.ex
index 409f734..b528dfe 100644
--- a/lib/tesla/middleware/follow_redirects.ex
+++ b/lib/tesla/middleware/follow_redirects.ex
@@ -1,39 +1,41 @@
defmodule Tesla.Middleware.FollowRedirects do
- @doc """
+ @moduledoc """
Follow 301/302 redirects
Example:
+ defmodule MyClient do
+ use Tesla
- defmodule MyClient do
- use Tesla
+ plug Tesla.Middleware.FollowRedirects, max_redirects: 3 # defaults to 5
+ end
- plug Tesla.Middleware.FollowRedirects, max_redirects: 3 # defaults to 5
- end
+ **NOTE** For `httpc` adapter you have to explicitely disable default autoredirection
+ adapter :httpc, autoredirect: false
"""
@max_redirects 5
@redirect_statuses [301, 302, 307, 308]
def call(env, next, opts \\ []) do
max = Keyword.get(opts || [], :max_redirects, @max_redirects)
redirect(env, next, max)
end
defp redirect(env, next, left) when left == 0 do
case Tesla.run(env, next) do
%{status: status} = env when not status in @redirect_statuses ->
env
_ ->
raise Tesla.Error, "too many redirects"
end
end
defp redirect(env, next, left) do
case Tesla.run(env, next) do
%{status: status, headers: %{"location" => location}} when status in @redirect_statuses ->
redirect(%{env | url: location}, next, left - 1)
env ->
env
end
end
end

File Metadata

Mime Type
text/x-diff
Expires
Tue, Jan 21, 5:31 AM (20 h, 35 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
55661
Default Alt Text
(1 KB)

Event Timeline