Francis.ResponseHandlers (Francis v0.2.0)

View Source

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

Summary

Functions

Sends an HTML response with a 200 status code and HTML content.

Sends an HTML response with the given status code and HTML content.

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 a 200 status code and HTML content.

Warning: The following function does not escape HTML content. Passing user-generated or untrusted input may result in Cross-Site Scripting (XSS) vulnerabilities. Only use this function with trusted, static HTML content. Look into phoenix_html

Examples

defmodule Example do
  use Francis

  get("/", fn conn ->
    html(conn, "<h1>Hello World!</h1>")
  end)
end

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.

Warning: The following function does not escape HTML content. Passing user-generated or untrusted input may result in Cross-Site Scripting (XSS) vulnerabilities. Only use this function with trusted, static HTML content. Look into phoenix_html

Examples

defmodule Example do
  use Francis

  get("/", fn conn ->
    html(conn, 201, "<h1>Created</h1>")
  end)
end

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

defmodule Example do
  use Francis

  get("/api/data", fn conn ->
    json(conn, %{message: "Success", data: [1, 2, 3]})
  end)
end

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

defmodule Example do
  use Francis

  post("/users", fn conn ->
    json(conn, 201, %{id: 123, message: "User created"})
  end)
end

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

defmodule Example do
  use Francis

  get("/old", fn conn -> redirect(conn, "/new") end)
end

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

defmodule Example do
  use Francis

  get("/old", fn conn -> redirect(conn, 301, "/new") end)
end

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

defmodule Example do
  use Francis

  get("/hello", fn conn ->
    text(conn, "Hello World!")
  end)
end

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

defmodule Example do
  use Francis

  get("/text", fn conn ->
    text(conn, 200, "Hello World!")
  end)
end