The methods body, status and headers

  1. It is possible and recommended to set the status code and response body with the return value of the route block.
  2. However, in some scenarios you might want to set the body at an arbitrary point in the execution flow.
  3. You can do so with the body helper method.
  4. If you do so, you can use that method from there on to access the body:
    get '/foo' do
      body "bar"
    after do
      puts body
    It is also possible to pass a block to body, which will be executed by the Rack handler (this can be used to implement streaming).

  5. Similar to the body, you can also set the status code and headers:

    get '/foo' do
      status 418
      headers \
        "Allow"   => "BREW, POST, GET, PROPFIND, WHEN",
        "Refresh" => "Refresh: 20;"
      body "I'm a tea pot!"
  6. Like body, headers and status with no arguments can be used to access their current values.

Casiano Rodriguez León 2015-01-07