kemal/docs/http-requests.md

40 lines
1.1 KiB
Markdown
Raw Normal View History

2015-12-16 18:32:01 +00:00
# HTTP Request / Response Lifecycle
2015-12-04 11:52:39 +00:00
2015-12-16 18:32:01 +00:00
Accessing the HTTP request/response environment (query params, body, content_type, headers, status_code) is super easy. You can use the environment returned from the block:
2015-12-04 11:52:39 +00:00
```ruby
2015-12-16 18:32:01 +00:00
# Matches /hello/kemal
2015-12-04 11:52:39 +00:00
get "/hello/:name" do |env|
name = env.params["name"]
"Hello back to #{name}"
end
# Matches /resize?width=200&height=200
get "/resize" do |env|
width = env.params["width"]
height = env.params["height"]
end
# 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["name"] as String
likes = env.params["likes"] as Array
"#{name} likes #{likes.each.join(',')}"
end
# Set the content as application/json and return JSON
get "/user.json" do |env|
kemal = {name: "Kemal", language: "Crystal"}
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