Skip to content

Responses

Mantis provides a simple way to return responses to clients.

HTML Responses

Return HTML content using response.html():

v
module main

import khalyomede.mantis.http { create_app, App, Response }
import khalyomede.mantis.http.route
import khalyomede.mantis.http.response

fn main() {
  app := create_app(
    routes: [
      route.get(
        name: "index"
        path: "/"
        callback: fn (app App) Response {
          return response.html(content: "hello world") 
        }
      )
    ]
  )

  app.serve() or { panic(err) }
}

Status Codes

Set HTTP status codes using the status field:

v
module main

import khalyomede.mantis.http { create_app, App, Response, Status }
import khalyomede.mantis.http.route
import khalyomede.mantis.http.response

fn main() {
  app := create_app(
    routes: [
      route.post(
        name: "post.store"
        path: "/post"
        callback: fn (app App) Response {
          return response.html(
            content: "Post created successfully"
            status: .created 
          )
        }
      )
    ]
  )

  app.serve() or { panic(err) }
}

Headers

Add custom headers to your response:

v
module main

import khalyomede.mantis.http { create_app, App, Response }
import khalyomede.mantis.http.route
import khalyomede.mantis.http.response

fn main() {
  app := create_app(
    routes: [
      route.get(
        name: "cache.example"
        path: "/cached-page"
        callback: fn (app App) Response {
          return response.html(
            content: "This page is cached"
            headers: { 
              'Cache-Control': ['max-age=3600']
            }
          )
        }
      )
    ]
  )

  app.serve() or { panic(err) }
}

Redirects

Redirect users to another URL:

v
module main

import khalyomede.mantis.http { create_app, App, Response }
import khalyomede.mantis.http.route
import khalyomede.mantis.http.response

fn main() {
  app := create_app(
    routes: [
      route.get(
        name: "profile"
        path: "/profile"
        callback: fn (app App) Response {
          return response.redirect("/login", {}) 
        }
      )
    ]
  )

  app.serve() or { panic(err) }
}

You can also add query parameters:

v
import khalyomede.mantis.http { create_app, App, Response }
import khalyomede.mantis.http.route
import khalyomede.mantis.http.response

fn main() {
  app := create_app(
    routes: [
      route.get(
        name: "admin"
        path: "/admin"
        callback: fn (app App) Response {
          return response.redirect("/login", { 
            'return_to': '/admin'
          })
        }
      )
    ]
  )

  app.serve() or { panic(err) }
}