Francis.ResponseHandlers (Francis v0.1.23)

View Source

A module providing functions to handle HTTP responses in a Plug application.

Summary

Functions

Sends an HTML response with the given status code and HTML content. It will escape the HTML content to prevent XSS attacks.

Sends an HTML response with the given status code and HTML content. It will escape the HTML content to prevent XSS attacks.

Sends a JSON response with a 200 status code and the given data.

Sends a JSON response with the given status code and data.

Redirects the connection to the specified path with a 302 status code.

Redirects the connection to the specified path with a custom status code.

Sends a text response with a 200 status code and the given text.

Sends a text response with the given status code and text.

Functions

html(conn, html)

@spec html(Plug.Conn.t(), String.t()) :: Plug.Conn.t()

Sends an HTML response with the given status code and HTML content. It will escape the HTML content to prevent XSS attacks.

Examples

html(conn, 200, "<h1>Hello World!</h1>")

html(conn, status, html)

@spec html(Plug.Conn.t(), integer(), String.t()) :: Plug.Conn.t()

Sends an HTML response with the given status code and HTML content. It will escape the HTML content to prevent XSS attacks.

Examples

html(conn, 200, "<h1>Hello World!</h1>")

json(conn, data)

@spec json(Plug.Conn.t(), map() | list()) :: Plug.Conn.t()

Sends a JSON response with a 200 status code and the given data.

Examples

json(conn, %{message: "Success"})

json(conn, status, data)

@spec json(Plug.Conn.t(), integer(), map() | list()) :: Plug.Conn.t()

Sends a JSON response with the given status code and data.

Examples

json(conn, 201, %{message: "Success"})

redirect(conn, path)

@spec redirect(Plug.Conn.t(), String.t()) :: Plug.Conn.t()

Redirects the connection to the specified path with a 302 status code.

Examples

redirect(conn, "/new_path")

redirect(conn, status, path)

@spec redirect(Plug.Conn.t(), integer(), String.t()) :: Plug.Conn.t()

Redirects the connection to the specified path with a custom status code.

Examples

redirect(conn, 301, "/new_path")

text(conn, text)

@spec text(Plug.Conn.t(), String.t()) :: Plug.Conn.t()

Sends a text response with a 200 status code and the given text.

Examples

text(conn, "Hello World!")

text(conn, status, text)

@spec text(Plug.Conn.t(), integer(), String.t()) :: Plug.Conn.t()

Sends a text response with the given status code and text.

Examples

text(conn, 200, "Hello World!")