Why doesn't HTTPoison return the HTML of the website?

When I run this:

HTTPoison.get! "www.example.com"

I get an %HTTPoison.Response struct which contains the body, headers and the status code. Normally the body should contain the actual response, but I almost always get an empty body, everything else in the response works fine. Here is what it looks like:

%HTTPoison.Response{
  body: "",
  headers: [
    {"Location",
     "http://megaplusredirection.tedata.net/VDSL-Redirection_90.html"},
    {"Connection", "close"}
  ],
  request: %HTTPoison.Request{ 
    body: "",
    headers: [],
    method: :get,
    options: [],
    params: %{},
    url: "http://www.example.com"
  },
  request_url: "http://www.example.com",
  status_code: 307

Notice how the body is an empty string.

Is this how HTTPoison is supposed to work or am I doing something wrong ?

Thanks in advance guys

It’s because the 307 status, that means the site has been temporary redirected to other location. The new location can be found in headers, here is http://megaplusredirection.tedata.net/VDSL-Redirection_90.html.

You can run HTTPosion.get! again to fetch data from the new location, or try to use [follow_redirect: true] option like this post: HTTPoison : Get body of redirected location target - #5 by Nicolas

2 Likes

Thanks a lot, I feel so stupid for not noticing that the status code, thanks so much.