Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTPoison.AsyncResponse id's can be {:maybe_redirect_response, status, headers, client} #424

Merged
merged 2 commits into from
Oct 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion lib/httpoison.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ defmodule HTTPoison.Request do
* `:socks5_user`- socks5 username
* `:socks5_pass`- socks5 password
* `:ssl` - SSL options supported by the `ssl` erlang module
* `:follow_redirect` - a boolean that causes redirects to be followed
* `:follow_redirect` - a boolean that causes redirects to be followed, can cause a request to return
a `MaybeRedirect` struct. See: HTTPoison.MaybeRedirect
* `:max_redirect` - an integer denoting the maximum number of redirects to follow. Default is 5
* `:params` - an enumerable consisting of two-item tuples that will be appended to the url as query string parameters
* `:max_body_length` - a non-negative integer denoting the max response body length. See :hackney.body/2
Expand Down Expand Up @@ -95,6 +96,30 @@ defmodule HTTPoison.AsyncEnd do
@type t :: %__MODULE__{id: reference}
end

defmodule HTTPoison.MaybeRedirect do
@moduledoc """
If the option `:follow_redirect` is given to a request, HTTP redirects are automatically follow if
the method is set to `:get` or `:head` and the response's `status_code` is `301`, `302` or `307`.

If the method is set to `:post`, then the only `status_code` that get's automatically
followed is `303`.

If any other method or `status_code` is returned, then this struct is returned in place of a
`HTTPoison.Response` or `HTTPoison.AsyncResponse`, containing the `redirect_url` to allow you
to optionally re-request with the method set to `:get`.
"""

defstruct status_code: nil, request_url: nil, request: nil, redirect_url: nil, headers: []

@type t :: %__MODULE__{
status_code: integer,
headers: list,
request: HTTPoison.Request.t(),
request_url: HTTPoison.Request.url(),
redirect_url: HTTPoison.Request.url()
}
end

defmodule HTTPoison.Error do
defexception reason: nil, id: nil
@type t :: %__MODULE__{id: reference | nil, reason: any}
Expand Down
Loading