HTTP.Response (http_fetch v0.5.0)
Represents an HTTP response with status, headers, body, and URL information.
Summary
Functions
Parses the Content-Type header to extract media type and parameters.
Gets a response header value by name (case-insensitive).
Parses the response body as JSON using Elixir's built-in JSON
module (available in Elixir 1.18+).
Reads the entire response body as binary.
Reads the response body and parses it as JSON.
Reads the response body as text.
Writes the response body to a file.
Types
@type t() :: %HTTP.Response{ body: String.t() | nil, headers: HTTP.Headers.t(), status: integer(), stream: pid() | nil, url: URI.t() }
Functions
Parses the Content-Type header to extract media type and parameters.
Examples
iex> response = %HTTP.Response{headers: HTTP.Headers.new([{"Content-Type", "application/json; charset=utf-8"}])}
iex> HTTP.Response.content_type(response)
{"application/json", %{"charset" => "utf-8"}}
iex> response = %HTTP.Response{headers: HTTP.Headers.new([{"Content-Type", "text/plain"}])}
iex> HTTP.Response.content_type(response)
{"text/plain", %{}}
Gets a response header value by name (case-insensitive).
Examples
iex> response = %HTTP.Response{headers: HTTP.Headers.new([{"Content-Type", "application/json"}])}
iex> HTTP.Response.get_header(response, "content-type")
"application/json"
iex> response = %HTTP.Response{headers: HTTP.Headers.new([{"Content-Type", "application/json"}])}
iex> HTTP.Response.get_header(response, "missing")
nil
Parses the response body as JSON using Elixir's built-in JSON
module (available in Elixir 1.18+).
Returns:
{:ok, map | list}
if the body is valid JSON.{:error, reason}
if the body cannot be parsed as JSON.
Note: This method is deprecated in favor of read_as_json/1
for streaming responses.
Reads the entire response body as binary.
For streaming responses, this will consume the entire stream into memory. For non-streaming responses, returns the existing body.
Examples
iex> response = %HTTP.Response{body: "Hello World", stream: nil}
iex> HTTP.Response.read_all(response)
"Hello World"
Reads the response body and parses it as JSON.
For streaming responses, this will read the entire stream before parsing.
Returns:
{:ok, map | list}
if the body is valid JSON.{:error, reason}
if the body cannot be parsed as JSON.
Examples
iex> response = %HTTP.Response{body: ~s({"key": "value"})}
iex> HTTP.Response.read_as_json(response)
{:ok, %{"key" => "value"}}
Reads the response body as text.
For streaming responses, this will read the entire stream into memory.
Writes the response body to a file.
For streaming responses, this will read the entire stream and write it to the file. For non-streaming responses, it will write the existing body directly.
Parameters
response
: The HTTP response to writefile_path
: The path to write the file to
Returns
:ok
on success{:error, reason}
on failure
Examples
iex> response = %HTTP.Response{body: "file content", stream: nil}
iex> HTTP.Response.write_to(response, "/tmp/test.txt")
:ok