Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling POSTDATA in Sinatra

I'm trying to create a page in Sinatra, so that whatever you post (under the parameter name "command") will be echoed back to you. Here's my current approach:

post '/eval' do
  "I got #{params[:data][:command]}."
end

If I try to post anything to /eval, it results in an internal server error. What am I doing wrong?

like image 325
Vineel Adusumilli Avatar asked Apr 29 '26 11:04

Vineel Adusumilli


1 Answers

The problem is that your [:data] param is nil. One way you could fix this is to remove the reference to [:data]. Try this instead.

require "rubygems"
require "sinatra"

post '/eval' do
  "I got #{params[:command]}."
end

you can test this with curl on your command line (if you are using a unix based system).

curl http://localhost:4567/eval -F "command=hello"

In the future, It will be helpful to others if you provide a stacktrace of your error with your question.

like image 126
sintaxi Avatar answered May 01 '26 16:05

sintaxi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!