Sha256: 50c8c3e4d983a3b544f1f266f40dc3a93985a60d8347591814a4987236a9d94e

Contents?: true

Size: 1.62 KB

Versions: 10

Compression:

Stored size: 1.62 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.header       # {} request header
    request.body         # request body sent by the client
    request.params       # {} params matched in router
  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'
  end
  # HTTP/1.1 200 OK
  # Server: Midori/1.0
  # Example-Header: Example-Value
  #
  # Hello
  
  get '/case_3' do
    status = 202
    header['Example-Header'] = 'Example-Value'
    Midori::Response.new(200, {}, 'Hello')
  end
  # HTTP/1.1 200 OK
  #
  # Hello
end
```

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
em-midori-0.2.4 tutorial/essentials/request_handling.md
em-midori-0.2.3 tutorial/essentials/request_handling.md
em-midori-0.2.2 tutorial/essentials/request_handling.md
em-midori-0.2.1 tutorial/essentials/request_handling.md
em-midori-0.2.0 tutorial/essentials/request_handling.md
em-midori-0.1.12 tutorial/essentials/request_handling.md
em-midori-0.1.11 tutorial/essentials/request_handling.md
em-midori-0.1.10 tutorial/essentials/request_handling.md
em-midori-0.1.9 tutorial/essentials/request_handling.md
em-midori-0.1.8 tutorial/essentials/request_handling.md