Handling POSTDATA in Sinatra

Vineel Adusumilli picture Vineel Adusumilli · Nov 14, 2010 · Viewed 8.8k times · Source

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?

Answer

sintaxi picture sintaxi · Nov 14, 2010

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.