Routes and Params
Handling HTTP.
Handling HTTP Requests
Web apps respond to requests by matching routes and reading parameters.
- Path segments can be dynamic
- Query strings carry extra data
- Form bodies carry posted fields
Sinatra exposes all of these through the params hash.
require 'sinatra'
get '/echo' do
params['msg'].to_s
endNamed Route Parameters
A colon in the path defines a named parameter that captures a path segment.
/users/:idmatches/users/42- The captured value appears in
params['id']
require 'sinatra'
get '/users/:id' do
"User ID is #{params['id']}"
endAll lessons in this course
- Sinatra Basics
- Routes and Params
- JSON APIs
- Middleware and Rack