Headers are specifications passed along in the response that are essential for setting up the Rack environment.
In order to understand the structure, we'll use HTTP protocols for simplicity. Everything you see in your browser, for example, is transferred via HTTP. Navigating the web is essentially a large series of HTTP requests and responses, and these are transported by HTTP headers in your metadata.
If you're using Firefox or Chrome, you can use the Live HTTP Headers extension to see what these HTTP Headers look like. Once you enable it to capture your requests and responses, you can click on a logged request/response and see the corresponding HTTP Headers.
Rack uses these headers to set up the environment in accordance with the HTTP standard. The header is simply a hash that contains these header elements. For example, the following is a Rack response for a ResponseTimer
Rack app:
class ResponseTimer
def initialize(app)
@app = app
end
def call(env)
[200, {"Content-Type" => "text/html"}, "Hello World!"]
end
end
The header in this response is {"Content-Type" => "text/html"}
. This is simply saying that the content type of this response is text/html
, and this will render the body Hello World!
in the HTML text formatting language.
View Headers on Learn.co and start learning to code for free.