Mystery in Ruby sinatra

Posted by JVK on Stack Overflow See other posts from Stack Overflow or by JVK
Published on 2012-12-10T22:47:08Z Indexed on 2012/12/10 23:04 UTC
Read the original article Hit count: 160

Filed under:
|

I have the following Sinatra code:

post '/bucket' do
  # determine if this call is coming from filling out web form
  is_html = request.content_type.to_s.downcase.eql?('application/x-www-form-urlencoded')

  # If this is a curl call, then get the params differently
  unless is_html
      params = JSON.parse(request.env["rack.input"].read)
  end

  p params[:name]
end

If I call this using Curl, params has values, but when this is called via a web form, then params is nil and params[:name] has nothing. I spent several hours figuring out why it happens and asked help from other people, but no one could really find out what is going on.

One thing to note is, if I comment out this line:

params = JSON.parse(request.env["rack.input"].read)

then params has the correct value for "web-form" posting.

Actually, the goal is to get the params value if this code is being called by CURL call, so I used:

params = JSON.parse(request.env["rack.input"].read)

but it messed up the web-form posting. Can anyone solve this mystery?

© Stack Overflow or respective owner

Related posts about ruby

Related posts about sinatra