Update environment documentation

This commit is contained in:
Sdogruyol 2015-10-30 22:40:37 +02:00
parent a7cc498dca
commit f82a57b2a4

View file

@ -63,9 +63,9 @@ In Kemal, a route is an HTTP method paired with a URL-matching pattern. Each rou
end
```
## Context
## Environment
Accessing the request environment (query params, body, headers e.g) is super easy. You can use the context returned from the block:
Accessing the environment (query params, body, content_type, headers, status_code) is super easy. You can use the environment returned from the block:
```ruby
# Matches /hello/kemal
@ -79,18 +79,19 @@ Accessing the request environment (query params, body, headers e.g) is super eas
width = env.params["width"]
height = env.params["height"]
end
```
## Content Type
Kemal uses *text/html* as the default content type. You can change it via the context.
```ruby
# Set the content as application/json and return JSON
get "/user.json" do |env|
kemal = {name: "Kemal", language: "Crystal"}
env.set_content_type "application/json"
env.content_type = "application/json"
kemal.to_json
end
# Add headers to your response
get "/headers" do |env|
env.add_header "Accept-Language", "tr"
env.add_header "Authorization", "Token 12345"
end
```
## Thanks