github facebook twitter

HTTP Request / Response Context

Accessing the HTTP request/response context (query params, body, content_type, headers, status_code) is super easy. You can use the context returned from the block:

# Matches /hello/kemal
get "/hello/:name" do |env|
  name = env.params.url["name"]
  "Hello back to #{name}"

# Matches /resize?width=200&height=200
get "/resize" do |env|
  width = env.params.query["width"]
  height = env.params.query["height"]

# Easily access JSON payload from the params.
# The request content type needs to be application/json
# The payload
# {"name": "Serdar", "likes": ["Ruby", "Crystal"]}
post "/json_params" do |env|
  name = env.params.json["name"].as(String)
  likes = env.params.json["likes"].as(Array)
  "#{name} likes #{likes.each.join(',')}"

# Set the content as application/json and return JSON
get "/user.json" do |env|
  user = {name: "Kemal", language: "Crystal"}.to_json
  env.response.content_type = "application/json"

# Add headers to your response
get "/headers" do |env|
  env.response.headers["Accept-Language"] = "tr"
  env.response.headers["Authorization"] = "Token 12345"

Context Storage

Context is pretty useful. You can use context to store some variables and access them later at some point. Each stored value only exist in the lifetime of request / response cycle. This pretty useful for sharing states between middlewares, filters e.g

before_get "/" do |env|
  env.set "is_kemal_cool", true

get "/" do |env|
  is_kemal_cool = env.get "is_kemal_cool"
  "Kemal cool = #{is_kemal_cool}"

This renders Kemal cool = true when a request is made to / :)

Request Properties

Some common request information is available at env.request.*:

  • method - the HTTP method
    • e.g. GET, POST, …
  • headers - a hash containing relevant request header information
  • body - the request body
  • version - the HTTP version
    • e.g. HTTP/1.1
  • path - the uri path
    • e.g. => /docs/context
  • resource - the uri path and query params
    • e.g. => /docs/context?lang=cr
  • cookies
    • e.g. env.request.cookies["cookie_name"].value