Sinatra makes it easy to access any particular incoming form field by name:
post "/" do
params['form_field_name']
end
But how does one enumerate over all the form fields in a request? I found nothing in the documentation. I even tried
request.body.split('&')
but request.body is an instance of StringIO, and not a string.
If params
is a hash, you can try:
params.keys.each do |k|
puts "#{k} - #{params[k]}"
end
I just discovered in Sinatra's excellent API docs that Sinatra::Request is a subclass of Rack::Request. The request object available to Sinatra handlers inherits has a POST method which returns a hash of submitted form fields.
request.POST.each { |k,v| puts "#{k} = #{v}" }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With