Sha256: da3808184637e9baab5be2ff480e6bbe4fe9c8a5552e579ac5d9a49e3bf5bc64
Contents?: true
Size: 1.83 KB
Versions: 4
Compression:
Stored size: 1.83 KB
Contents
# Request Handling ## Accessing the request object The incoming request object can be accessed from request level (filter, routes, error handlers) through the `request` method: ```ruby class ExampleAPI < Midori::API get '/' do request.ip # '127.0.0.1' client ip address request.port # '8080' client port request.method # 'GET' request.path # '/' request.query_string # '' request.query_params # {} query params, parsed from query string request.header # {} request header request.body # request body sent by the client request.params # {} params matched in router request.cookie # {} cookie parsed from header end end ``` **Note (for sinatra users): **The `request.body` in modori is a `String` object but not a `StringIO` object. ## Construct the response object Midori accepts the return value of the block as the response body by default. You could edit variable `status` and `header` to construct things other than body. You could also return a `Midori::Response` object as your response, which could override everything. ```ruby class ExampleAPI < Midori::API get '/case_0' do 'Hello' # HTTP/1.1 200 OK # Server: Midori/1.0 # # Hello end get '/case_1' do @status = 418 "I\'m a teapot" # HTTP/1.1 418 I'm a teapot # Server: Midori/1.0 # # I'm a teapot end get '/case_2' do @header['Example-Header'] = 'Example-Value' 'Hello' # HTTP/1.1 200 OK # Server: Midori/1.0 # Example-Header: Example-Value # # Hello end get '/case_3' do @status = 202 @header['Example-Header'] = 'Example-Value' Midori::Response.new(status: 200, header: {}, body: 'Hello') # Overrides everything else # HTTP/1.1 200 OK # Server: Midori/1.0 # # Hello end end ```
Version data entries
4 entries across 4 versions & 1 rubygems