Responses (appendix v0.0.2)
A module providing wrappers for common HTTP responses in Plug applications.
This module includes functions to send JSON, HTML, text, file responses, and more. It also provides utilities for handling Ecto changeset errors and redirecting requests.
Summary
Functions
Sets up common imports for modules using Responses
.
Converts string keys in params to atoms.
Sends a file download response.
Sends a file response.
Sends an HTML response.
Sends a JSON response.
Parses errors from an Ecto changeset or a given error map/string.
Redirects the request.
Sends a status-only response.
Sends a text response.
Functions
Sets up common imports for modules using Responses
.
This macro imports Plug.Conn
and the current module for easy access to response helpers.
Examples
defmodule MyRouter do
use Plug.Router
use Responses
plug :match
plug :dispatch
get "/" do
conn |> json(:ok, %{hello: "world"})
end
end
changeset_params(params \\ %{})
Converts string keys in params to atoms.
Parameters
- params: A map with string keys.
Examples
changeset_params(%{"name" => "John"})
# => %{name: "John"}
download(conn, path, opts \\ [])
Sends a file download response.
Parameters
- conn: HTTP connection to send the response to.
- path: File path for the file response.
- opts: Keyword list of options. Supports
:resp_headers
to add response headers.
Examples
conn |> download(:ok, "/path/to/file")
conn |> download(:ok, "/path/to/file", resp_headers: %{"x-foo" => "bar"})
file(conn, path, opts \\ [])
Sends a file response.
Parameters
- conn: HTTP connection to send the response to.
- path: File path for the file response.
- opts: Keyword list of options. Supports
:resp_headers
to add response headers.
Examples
conn |> file(:ok, "/path/to/file")
conn |> file(:ok, "/path/to/file", resp_headers: %{"x-foo" => "bar"})
html(conn, status, data, opts \\ [])
Sends an HTML response.
Parameters
- conn: HTTP connection to send the response to.
- status: Atom or number that represents the HTTP response code.
- data: HTML string set as the response body.
- opts: Keyword list of options. Supports
:resp_headers
to add response headers.
Examples
conn |> html(:ok, "<h1>Hello World</h1>")
conn |> html(:ok, "<h1>Hello World</h1>", resp_headers: %{"x-foo" => "bar"})
json(conn, status, data, opts \\ [])
Sends a JSON response.
Parameters
- conn: HTTP connection to send the response to.
- status: Atom or number that represents the HTTP response code.
- data: Struct to be encoded to JSON as the response body.
- opts: Keyword list of options. Supports
:resp_headers
to add response headers.
Examples
conn |> json(:ok, %{hello: "World"})
conn |> json(:ok, %{hello: "World"}, resp_headers: %{"x-foo" => "bar"})
parse_errors(changeset)
Parses errors from an Ecto changeset or a given error map/string.
Parameters
- changeset: An Ecto changeset.
- errors: An error map or string.
Examples
parse_errors(%Ecto.Changeset{errors: [name: {"can't be blank", []}]})
# => %{name: "can't be blank"}
parse_errors("An error occurred")
# => %{error: "An error occurred"}
redirect(conn, status \\ 302, url)
Redirects the request.
Parameters
- conn: HTTP connection to redirect.
- status: Atom or number that represents the HTTP response code.
- url: String representing the URL/URI destination.
Examples
conn |> redirect("http://example.com/")
conn |> redirect(301, "http://example.com/")
status(conn, status, opts \\ [])
Sends a status-only response.
Parameters
- conn: HTTP connection to send the response to.
- status: Atom or number that represents the HTTP response code.
- opts: Keyword list of options. Supports
:resp_headers
to add response headers.
Example
conn |> status(:ok)
text(conn, status, data, opts \\ [])
Sends a text response.
Parameters
- conn: HTTP connection to send the response to.
- status: Atom or number that represents the HTTP response code.
- data: Text string set as the response body.
- opts: Keyword list of options. Supports
:resp_headers
to add response headers.
Examples
conn |> text(:ok, "Hello World!")
conn |> text(:ok, "Hello World!", resp_headers: %{"x-foo" => "bar"})