Skip to content
This repository has been archived by the owner on Dec 7, 2018. It is now read-only.

Requests

Tony Arcieri edited this page Sep 15, 2013 · 21 revisions

See also: Reel::Request and Reel::RequestMixin Yardocs

Reel::Request objects represent an incoming HTTP request from a client. The following methods can be used to obtain the request properties:

HTTP method

>> request.method
=> "GET"

Requested URL

>> request.url
=> "/foobar?baz=quux"

Requested URI (as a Ruby URI object)

>> request.uri
=> #<URI::Generic:0x007ff70b8ba588 URL:/foobar?baz=quux>

Requested path (without query string)

>> request.path
=> "/foobar"

Requested query string

>> request.query_string
=> "baz=quux"

Request headers

>> request.headers
=> {"User-Agent"=>
  "curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8x zlib/1.2.5",
 "Host"=>"localhost:3000",
 "Accept"=>"*/*"}

# shorthand
>> request["Host"] 
=> "localhost:3000"

HTTP version requested

>> request.version
=> "1.1"

Is this a Websocket request?

> request.websocket?
=> true

Request body

>> request.body
=> #<Reel::RequestBody:3fd5a89e1cc8 @streaming=false>

Using Reel::RequestBodies

See also: Reel::RequestBody Yardoc

Reel::RequestBody is an object representing an incoming request body. Unlike frameworks like Rack (which mandates "rewindable input"), Reel has been proactively designed to support the streaming of request bodies, allowing you to handle them incrementally as they're uploaded to the server. It also supports methods for quickly consuming the request body as a string.

Obtaining the request body as a String

>> request.body.to_s
=> "hello, world!"

Streaming the request body with #readpartial

The #readpartial method of Reel::RequestBody can be used to incrementally stream the body:

>> request.body.readpartial(3)
=> "hel"
>> request.body.readpartial(5)
=> "lo,wo"
>> request.body.readpartial(10)
=> "rld!"
>> request.body.readpartial(10)
=> nil

Streaming the body as a Enumerable

Reel::RequestBody is an Enumerable, so you can use the #each method to iterate over it:

>> request.body.each { |chunk| puts chunk }
hello,
world!
=> nil