sinatra-param
sinatra-param copied to clipboard
Add example of PostBodyContentTypeParser use for JSON validation
Could you please provide an example of the following?
Use sinatra-param in combination with Rack::PostBodyContentTypeParser and Rack::NestedParams to automatically parameterize JSON POST bodies and nested parameters.
Been looking all over for an example of this, and I can't find anything.
Thanks in advance!
# Gemfile
gem 'rack-contrib', require: false
gem 'sinatra-contrib', require: false
# app.rb
require 'rack/contrib'
require 'sinatra/base'
class Api < Sinatra::Base
use Rack::PostBodyContentTypeParser
post "/" do
puts "Params automatically converted to a hash if request Content-Type is application/json:"
puts params
end
end
# Gemfile gem 'rack-contrib', require: false gem 'sinatra-contrib', require: false# app.rb require 'rack/contrib' require 'sinatra/base' class Api < Sinatra::Base use Rack::PostBodyContentTypeParser post "/" do puts "Params automatically converted to a hash if request Content-Type is application/json:" puts params end end
gem 'sinatra-contrib', require: false is not needed.
Apart from that, you can also only load the specific component you need like this: require 'rack/contrib/post_body_content_type_parser'