Sinatra 无法响应非常基本的 GET 请求

发布于 2024-12-26 12:01:59 字数 1274 浏览 5 评论 0原文

我有以下 Gemfile:

source "http://rubygems.org"

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

和以下 Sinatra 应用程序:

require 'sinatra'
require 'json'

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

当我提出这样的基本请求时:

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

我得到:

[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

我在 Lion Mac 中使用带有 RVM 的 Ruby 1.9.2。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧伤慢歌 2025-01-02 12:01:59

您尝试从路由返回哈希值,但哈希值不是 您可以的东西返回此处

只需使用 .to_json 将哈希值转换为 json 字符串,然后您可以返回该字符串(您需要 json,但尚未使用它):

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文