HTTP Context

The `ctx` that is passed to all of the middlewares and blocks is simply Crystal's own [HTTP::Server::Context](https://crystal-lang.org/api/0.21.1/HTTP/Server/Context.html) with some additions.

Raze Context Additions

The following properties are added to the Crystal's HTTP::Server::Context for your convenience:

URL Params

Named URL parameters can be extracted via the params hash.

get "/user/:username" do |ctx|
  username = ctx.params["username"]
  "hello, #{username}"
end

# https://localhost:7777/user/orgalorg
# => "hello, orgalorg"

Query Params

Crystal automatically parses the query string, which can be found at ctx.request.query_params. The query property is literally a reference to the query_params, and can be accessed like so:

get "/superhero" do |ctx|
  special_ability = ctx.query["ability"]?
  if special_ability
    "your special ability: #{special_ability}"
  else
    "no special abilities given"
  end
end

# https://localhost:7777/superhero?ability=eating%20metal
# => "your special ability: eating metal"

User State

Custom state that should transfered from handler to handler can be placed in the State hash

class UserAuthenticator < Raze::Handler
  def call(ctx, done)
    ctx.state["user_id"] = "user_123"
    done.call
  end
end

get "/user" do |ctx|
  "user id is #{ctx.state["user_id"]}"
end

# https://localhost:7777/user
# => "user id is user_123"

Redirecting

Raze adds a redirect method directly to the context.

get "/raze" do |ctx|
  ctx.redirect "http://www.razecr.com"
end

Crystal's HTTP::Server::Context Properties

If you want to see what else Crystal has for you on the context, you can go to the Crystal API docs, but we will list some of the (likely) more useful ones that aren't already directly accessible on the context itself: