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

pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
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
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
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
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New

We're in Beta

About us Mission Statement