Nicolas

Nicolas

HTTPoison : Get body of redirected location target

Hi,

I have this URL https://beerfactory.org/api/v1/apps which redirects (HTTP 301) to https://mastodon.beerfactory.org/api/v1/apps.

When using HTTPoison to post of this URL with the following code to follow the redirection:

HTTPoison.request(:post, "https://beerfactory.org/api/v1/apps", "", [], [follow_redirect: true])

I get the following result :

{:ok,
 %HTTPoison.AsyncResponse{
   id: {:maybe_redirect, 301,
    [
      {"Server", "nginx/1.14.0 (Ubuntu)"},
      {"Date", "Mon, 29 Oct 2018 21:45:22 GMT"},
      {"Content-Type", "text/html"},
      {"Content-Length", "194"},
      {"Connection", "keep-alive"},
      {"Location", "https://mastodon.beerfactory.org/api/v1/apps"}
    ],
    {:client, {1540, 849522, 427514}, {:metrics_ng, :metrics_dummy},
     :hackney_ssl, 'beerfactory.org', 443, "beerfactory.org",
     [max_redirect: 5, follow_redirect: true],
     {:sslsocket, {:gen_tcp, #Port<0.7>, :tls_connection, :undefined},
      [#PID<0.204.0>, #PID<0.203.0>]},
     {:default, #Reference<0.4243972227.2875195393.28572>,
      {'beerfactory.org', 443, :hackney_ssl}, #PID<0.199.0>, :hackney_ssl},
     #Reference<0.4243972227.2875195393.28572>, true, :hackney_pool, 5000, true,
     5, false, 5, nil, nil,
     {:hparser, :response, 4096, 10, 0, :on_body,
      "<html>\r\n<head><title>301 Moved Permanently</title></head>\r\n<body bgcolor=\"white\">\r\n<center><h1>301 Moved Permanently</h1></center>\r\n<hr><center>nginx/1.14.0 (Ubuntu)</center>\r\n</body>\r\n</html>\r\n",
      {1, 1}, "", [], 194, "", "keep-alive", "text/html",
      "https://mastodon.beerfactory.org/api/v1/apps", :waiting},
     {6,
      {:dict, 6, 16, 16, 8, 80, 48,
       {[], [], [], [], [], [], [], [], [], [], [], ...},
       {{[["content-length", {3, "Content-Length", "194"}]],
         [
           [
             "location",
             {5, "Location", "https://mastodon.beerfactory.org/api/v1/apps"}
           ]
         ], [["date", {1, "Date", "Mon, 29 Oct 2018 21:45:22 GMT"}]], [], [],
         [["server", {...}]], [], [], [], ...}}}}, :connected, :waiting, nil,
     :normal, false, false, false, :undefined, false, &:hackney_request.send/2,
     :waiting, nil, 4096, "", [], {1, 1}, 194, nil, nil, "POST", "/api/v1/apps",
     ...}}
 }}

Is there a way I can get the redirection target body from this result ?

Thanks.

Marked As Solved

Nicolas

Nicolas

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.

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New

We're in Beta

About us Mission Statement