Sinatra fails to answer to a very basic GET request

1k views Asked by At

I have the following Gemfile:

source "http://rubygems.org"

gem 'sinatra', '1.3.2'
gem 'json', '1.6.4'

And the following Sinatra application:

require 'sinatra'
require 'json'

get '/ze/api/session.json' do
  content_type :json
  { :name => 'name' }
end

And when I make a basic request like this one:

curl localhost:4567/ze/api/session.json

I get this:

[2012-01-13 17:30:36] ERROR TypeError: can't convert Array into String
/Users/mauricio/.rvm/gems/ruby-1.9.2-p290@office-drop-sync/gems/rack-1.4.0/lib/rack/handler/webrick.rb:72:in `block in service'
/Users/mauricio/.rvm/gems/ruby-1.9.2-p290@office-drop-sync/gems/rack-1.4.0/lib/rack/handler/webrick.rb:71:in `each'
/Users/mauricio/.rvm/gems/ruby-1.9.2-p290@office-drop-sync/gems/rack-1.4.0/lib/rack/handler/webrick.rb:71:in `service'
/Users/mauricio/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
/Users/mauricio/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
/Users/mauricio/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'
localhost - - [13/Jan/2012:17:30:36 BRT] "GET /ze/api/session.json HTTP/1.1" 500 311

I am using Ruby 1.9.2 with RVM in a Lion Mac.

1

There are 1 answers

1
matt On BEST ANSWER

You're trying to return a hash from the route, but a hash isn't something you can return here.

Simply use .to_json to turn the hash into a json string which you can then return (you've required json, but aren't using it yet):

get '/ze/api/session.json' do
  content_type :json
  { :name => 'name' }.to_json
end