Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example of PostBodyContentTypeParser use for JSON validation #89

Open
lukenewman opened this issue Jul 8, 2018 · 2 comments
Open

Comments

@lukenewman
Copy link

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!

@vifreefly
Copy link

# 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

@pjmartorell
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants