Skip to content

Responses ​

An action responds by calling one render method — usually as the last expression in call.

JSON ​

crystal
render_json({id: post.id, title: post.title})
render_json(posts.map { |post| {id: post.id, title: post.title} })
render_json({error: "conflict"}, status_code: 409)

Anything with a to_json method works: named tuples, hashes, arrays, and your own types that include JSON::Serializable. The Content-Type is application/json.

For larger or reused JSON shapes, use a Jbuilder template instead.

Plain text ​

crystal
render_plain "pong"
render_plain "Not here", status_code: 404

Sets Content-Type: text/plain.

A body with any content type ​

crystal
render_body csv_string, content_type: "text/csv"
render_body "<h1>Hello</h1>", content_type: "text/html", status_code: 200

content_type: is optional; without it, no Content-Type header is set.

Files ​

crystal
render_file "storage/reports/2026-q3.pdf",
  content_type: "application/pdf",
  filename: "report.pdf"              # suggested download name
OptionDefaultDescription
content_type:requiredThe file's MIME type
filename:nilDownload name in Content-Disposition
disposition:"attachment"Use "inline" to display in the browser
status_code:200

The path is resolved relative to the working directory. Never pass a path built from user input without checking it. To serve a whole directory of public assets, use Static Files instead.

Redirects ​

crystal
redirect "/posts"                              # 302 Found
redirect "/posts/#{post.id}", status_code: 303 # See Other, after a POST
redirect "https://example.com", status_code: 301

Empty responses ​

render_empty sends no body. Use it for 204 No Content, 304 Not Modified and HEAD responses:

crystal
class Posts::DeletePost < Blog::BaseAction
  delete "/posts/:id"

  call do
    input = validate(PostPathContract)
    if store.delete_post(id: input.id) > 0   # rows deleted
      render_empty status_code: 204
    else
      render_json({error: "post_not_found"}, status_code: 404)
    end
  end
end

Runcobo never strips a body for you based on the method or status code — you decide. A route that answers both GET and HEAD:

crystal
require "digest/sha1"

class Posts::ExportPost < Blog::BaseAction
  get "/posts/:id/export"
  head "/posts/:id/export"

  call do
    input = validate(PostPathContract)
    if post = store.get_post?(id: input.id)
      body = "#{post.title}\n"
      etag = %("#{Digest::SHA1.hexdigest(body)}")
      context.response.headers["ETag"] = etag
      if request.method == "HEAD"
        context.response.content_length = body.bytesize
        render_empty content_type: "text/plain"
      elsif request.headers["If-None-Match"]? == etag
        render_empty status_code: 304
      else
        render_body body, content_type: "text/plain"
      end
    else
      render_empty status_code: 404
    end
  end
end

Templates ​

render renders a Jbuilder or Water template chosen by naming convention. See Views.

Headers, cookies and status ​

Use context.response for anything the render methods don't cover. Set headers before rendering — once the body starts, headers are sent:

crystal
call do
  context.response.headers["Cache-Control"] = "private, max-age=60"
  context.response.cookies << HTTP::Cookie.new("theme", "dark", http_only: true)
  render_json({ok: true})
end

Summary ​

MethodContent-TypeDefault status
render_json(value)application/json200
render_plain(text)text/plain200
render_body(body, content_type: nil)as given200
render_file(path, content_type:)as given200
render_empty(content_type: nil)as given200
redirect(url)—302
renderfrom the template engine200

All of them accept status_code:.

Released under the MIT License.