HTTPoison : Get body of redirected location target

OK, reading hackney documentation and source code it appears that hackney doesn’t follow 301/302 redirection unless you add force_redirect: true in the options:
In my example, the redirection is followed when both follow_redirect and force_redirect are specified:

HTTPoison.request(:post, "https://beerfactory.org/api/v1/apps", "", [], [follow_redirect: true, hackney: [{:force_redirect, true}]])                          
{:ok,
 %HTTPoison.Response{
   body: "{\"error\":\"Validation failed: Application name can't be blank, Redirect URI can't be blank\"}",
   headers: [
     {"Date", "Tue, 30 Oct 2018 08:00:42 GMT"},
     {"Content-Type", "application/json; charset=utf-8"},
     {"Transfer-Encoding", "chunked"},
     {"Connection", "keep-alive"},
     {"Server", "Mastodon"},
     {"X-Frame-Options", "DENY"},
     {"X-Content-Type-Options", "nosniff"},
     {"X-XSS-Protection", "1; mode=block"},
     {"X-RateLimit-Limit", "7500"},
     {"X-RateLimit-Remaining", "7499"},
     {"X-RateLimit-Reset", "2018-10-30T08:05:00.943912Z"},
     {"Vary", "Accept-Encoding, Origin"},
     {"Cache-Control", "no-cache"},
     {"X-Request-Id", "535f3d45-e48c-4a32-8a5a-92ccb72d6f21"},
     {"X-Runtime", "0.028489"}
   ],
   request: %HTTPoison.Request{
     body: "",
     headers: [],
     method: :post,
     options: [follow_redirect: true, hackney: [force_redirect: true]],
     params: %{},
     url: "https://beerfactory.org/api/v1/apps"
   },
   request_url: "https://beerfactory.org/api/v1/apps",
   status_code: 422
 }}

422 status is due to the bad request format used for this example.

2 Likes