sinatra-param icon indicating copy to clipboard operation
sinatra-param copied to clipboard

Add example of PostBodyContentTypeParser use for JSON validation

Open lukenewman opened this issue 7 years ago • 2 comments

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!

lukenewman avatar Jul 08 '18 16:07 lukenewman

# 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

vifreefly avatar Nov 15 '18 08:11 vifreefly

# 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'

pjmartorell avatar Mar 13 '20 09:03 pjmartorell