Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Document request object. Fixes GH #60.

  • Loading branch information...
commit a51e2f8f4e434dac9f657f7e4a2790fd16c0abc6 1 parent f343874
@rkh rkh authored
Showing with 46 additions and 0 deletions.
  1. +46 −0 README.rdoc
View
46 README.rdoc
@@ -422,6 +422,52 @@ A route can punt processing to the next matching route using <tt>pass</tt>:
The route block is immediately exited and control continues with the next
matching route. If no matching route is found, a 404 is returned.
+== Accessing the Request Object
+
+The incoming request object can be accessed form request level (filter, routes, error handlers) through the `request` method:
+
+ # app running on http://example.com/example
+ get '/foo' do
+ request.body # request body sent by the client (see below)
+ request.scheme # "http"
+ request.script_name # "/example"
+ request.path_info # "/foo"
+ request.port # 80
+ request.request_method # "GET"
+ request.query_string # ""
+ request.content_length # length of request.body
+ request.media_type # media type of request.body
+ request.host # "example.com"
+ request.get? # true (similar methods for other verbs)
+ request.form_data? # false
+ request["SOME_HEADER"] # value of SOME_HEADER header
+ request.referer # the referer of the client or '/'
+ request.user_agent # user agent (used by :agent condition)
+ request.cookies # hash of browser cookies
+ request.xhr? # is this an ajax request?
+ request.url # "http://example.com/example/foo"
+ request.path # "/example/foo"
+ request.ip # client IP address
+ request.secure? # false
+ requuest.env # raw env hash handed in by Rack
+ end
+
+Some options, like `script_name` or `path_info` can also be written:
+
+ before { request.path_info = "/" }
+
+ get "/" do
+ "all requests end up here"
+ end
+
+The <tt>request.body</tt> is an IO or StringIO object:
+
+ post "/api" do
+ request.body.rewind # in case someone already read it
+ data = JSON.parse request.body.read
+ "Hello #{data['name']}!"
+ end
+
== Configuration
Run once, at startup, in any environment:
Please sign in to comment.
Something went wrong with that request. Please try again.