Httpoison respones explained

can someone explain the fields in body of httpoison response

{:ok,
 %HTTPoison.Response{body: <<31, 139, 8, 0, 0, 0, 0, 0, 0, 3, 237, 125, 121,
111, 219, 88, 182, 231, 87, 81, 121, 254, 233, 6, 204, 72, 20, 181, 22, 30,
166, 224, 53, 222, 109, 121, 143, 31, 6, 198, 149, 68, 137, 52, 37, 94, 153,
164, ...>>,
  headers: [{"Server", "nginx/1.11.4"},
   {"Date", "Tue, 10 Oct 2017 23:35:36 GMT"},
   {"Content-Type", "application/json; charset=utf-8"},
   {"Transfer-Encoding", "chunked"}, {"Connection", "keep-alive"},
   {"cache-control", "max-age=300"},
   {"x-request-id", "7qcnn88qvaf12tuc2u88nkqoj5odk19v"},
   {"Expires", "Tue, 10 Oct 2017 23:40:36 GMT"}, {"Content-Encoding", "gzip"},
   {"Access-Control-Allow-Origin", "*"}],
  request_url: "https://url",
  status_code: code}}

when i decode the body i get

%Diplomat.Proto.Status{
  code: 3, 
  details: [],
  message: " ERROR ENCOUNTERED IN CASE OF 4** series errors...\n    "
}

please explain these fields code:, details:, message: in the body
i can see that message gives detailed error in case of 4** series http responses but can anyone explain for other status codes

The problem is not in HTTPoison. You are getting some framework-specific (likely called Diplomat) body response. You should consult their docs.

3 Likes

this body is from the server (correct me if i’m wrong). i encountered this case while using Diplomat elixir library. can you explain the fields in body if some are from server response
i saw another example here

Please give more context to your question…

We don’t know what You are doing, nor what You have tried.

2 Likes

Sure, it’s pretty direct:

So it got a response from the server.

So it returned this struct of all the response data from the server, this is broken up into a set of keys:

The body key is the body of the request, the ‘data’ that the server returned, whether a webpage or whatever, in this case it’s a gzip file since it is gzipped content, the internal content is json it seems based on the header:

This is just a list of headers, the ‘metadata’ of the body, the server gives whatever it wants here, but there are a set of standards of course.

This was the URL that was requested.

This is the integral status code from the server. Generally this is 200 for generic success, but servers can return a variety of codes here meaning different things, all normal HTTP.

Decode? You mean ungzip correct? Upon doing so you should get a json file, I have no clue how you get a struct:

I’ve no clue what this is, never heard of Diplomat, HTTPoison has nothing called Diplomat in it, no clue what this part has to do with HTTPoison or how it appeared at all as none of the code you showed could create this. o.O

The body of the HTTPoison request is the result from the server, it has nothing to do with HTTPoison at all, it is only and entirely related to the server request itself as it is just the data that the server itself sent. So explaining this in the context of HTTPoison can be summed up as “This is data the server sent back to you”, nothing to do with HTTPoison itself at all.

This is all related to the server and the server only, nothing related to HTTPoison.

What specifically were you needing described from HTTPoison? Server information we cannot describe as that is all about the server you are connecting to and nothing to do with HTTPoison at all, but if you elaborate on what precisely from HTTPoison you need described then we can describe it. :slight_smile:

4 Likes

thank you for explaining.

i got this using dipomat library function Diplomat.Proto.Status.decode /1

%Diplomat.Proto.Status{
  code: 3, 
  details: [],
  message: " ERROR ENCOUNTERED IN CASE OF 4** series errors...\n    "
}

sorry for not using my headers

headers: [
     {"Vary", "X-Origin"},
     {"Vary", "Referer"},
     {"Content-Type", "application/x-protobuf"},
     {"Date", "Date"},
     {"Server", "ESF"},
     {"Cache-Control", "private"},
     {"X-XSS-Protection", "1; mode=block"},
     {"X-Frame-Options", "SAMEORIGIN"},
     {"X-Content-Type-Options", "nosniff"},
     {"Accept-Ranges", "none"},
     {"Vary", "Origin,Accept-Encoding"},
     {"Transfer-Encoding", "chunked"} ]

when diplomat gets x-protobuf content, the status_code and status are lost. can you explain this.

Found the code values here and their meaning

OK = 0,
  CANCELLED = 1,
  UNKNOWN = 2,
  INVALID_ARGUMENT = 3,
  DEADLINE_EXCEEDED = 4,
  NOT_FOUND = 5,
  ALREADY_EXISTS = 6,
  PERMISSION_DENIED = 7,
  UNAUTHENTICATED = 16,
  RESOURCE_EXHAUSTED = 8,
  FAILED_PRECONDITION = 9,
  ABORTED = 10,
  OUT_OF_RANGE = 11,
  UNIMPLEMENTED = 12,
  INTERNAL = 13,
  UNAVAILABLE = 14,
  DATA_LOSS = 15
1 Like