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

t()

@type t() :: %HTTP.Response{
  body: String.t() | nil,
  headers: HTTP.Headers.t(),
  status: integer(),
  stream: pid() | nil,
  url: URI.t()
}

Functions

content_type(response)

@spec content_type(t()) :: {String.t(), map()}

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", %{}}

get_header(response, name)

@spec get_header(t(), String.t()) :: String.t() | nil

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

json(response)

@spec json(t()) :: {:ok, map() | list()} | {:error, term()}

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.

read_all(response)

@spec read_all(t()) :: String.t()

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"

read_as_json(response)

@spec read_as_json(t()) :: {:ok, map() | list()} | {:error, term()}

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"}}

text(response)

@spec text(t()) :: String.t()

Reads the response body as text.

For streaming responses, this will read the entire stream into memory.

write_to(response, file_path)

@spec write_to(t(), String.t()) :: :ok | {:error, term()}

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 write
  • file_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