View Source BrowseyHttp.Response (BrowseyHttp v0.0.7)

A response from a browser-imitating HTTP request.

Summary

Types

Headers sent with a request or returned in a response from the server.

t()

Fields on a response

Functions

Converts our headers from a map to a list of 2-tuples in the format used by Finch or HTTPoison.

True if the response appears to be HTML, either based on its headers or its body content.

The original URL passed to BrowseyHttp.get/2, before any redirects.

Converts headers in the 2-tuple format used by Finch or HTTPoison to a map used by BrowseyHttp.get/2.

The human readable title for an HTTP error status code.

Types

@type headers() :: %{optional(binary()) => [binary()]}

Headers sent with a request or returned in a response from the server.

Maps response header names (all lowercase, like "content-encoding") to the values associated with that header. This is structured as a map, the way the Req library does, to make it clear that servers may legitimately send multiple values for the same header name.

You can use BrowseyHttp.Response.headers_to_proplist/1 to convert this to the format used by HTTP clients like Finch or HTTPoison, and you can use BrowseyHttp.Response.proplist_to_headers/1 to convert from that format to this one.

@type t() :: %BrowseyHttp.Response{
  body: binary(),
  final_uri: URI.t(),
  headers: headers(),
  runtime_ms: timeout(),
  status: non_neg_integer(),
  uri_sequence: [URI.t(), ...]
}

Fields on a response:

  • :body: the response body. For HTML documents, this will always be a String.t(), but for binary files like images and videos, it will be non-Unicode binary data.
  • :headers: a map from response header names (all lowercase, like "content-encoding") to the values associated with that header. This is structured as a map, the way the Req library does, to make it clear that servers may legitimately send multiple values for the same header name.
  • :status: the HTTP status code returned by the final URL in the chain of redirects, like 200 or 404.
  • :final_uri: the final URL in the chain of redirects, as a URI.
  • :uri_sequence: the complete chain of URLs that were visited (length 1 if there were no redirects), as a list of URIs. The first element will always be URL that was passed to BrowseyHttp.get/2, and the last will always be the :final_uri.
  • :runtime_ms: the number of milliseconds the request took to complete, including all redirects.

Functions

Link to this function

headers_to_proplist(response_or_headers)

View Source
@spec headers_to_proplist(t() | has_headers() | headers()) :: [
  {String.t(), String.t()}
]

Converts our headers from a map to a list of 2-tuples in the format used by Finch or HTTPoison.

Examples

iex> BrowseyHttp.Response.headers_to_proplist(%{body: "...", headers: %{"content-type" => ["text/html"]}})
[{"content-type", "text/html"}]

iex> BrowseyHttp.Response.headers_to_proplist(%{body: "...", headers: %{"content-encoding" => ["gzip", "br"]}})
[{"content-encoding", "gzip"}, {"content-encoding", "br"}]
@spec html?(has_headers_and_body()) :: boolean()

True if the response appears to be HTML, either based on its headers or its body content.

@spec original_uri(t() | has_uri_sequence()) :: URI.t()

The original URL passed to BrowseyHttp.get/2, before any redirects.

Link to this function

proplist_to_headers(proplist_headers)

View Source
@spec proplist_to_headers([{String.t(), String.t()}]) :: headers()

Converts headers in the 2-tuple format used by Finch or HTTPoison to a map used by BrowseyHttp.get/2.

Examples

iex> BrowseyHttp.Response.proplist_to_headers([{"Content-Type", "text/html"}])
%{"content-type" => ["text/html"]}

iex> BrowseyHttp.Response.proplist_to_headers([{"content-encoding", "gzip"}, {"content-encoding", "br"}])
%{"content-encoding" => ["gzip", "br"]}
@spec status_name(non_neg_integer()) :: String.t()

The human readable title for an HTTP error status code.