【问题标题】:XML instead of JSON with Grape, Rails 3 and Heroku使用 Grape、Rails 3 和 Heroku 的 XML 代替 JSON
【发布时间】:2012-04-05 07:52:25
【问题描述】:

我在 Rails 3 中使用 Grape (https://github.com/intridea/grape),但遇到了一个奇怪的问题。

我在我的 API 类中将 json 定义为默认输出格式,并且我使用 as_json 方法来输出我的结果。

在我的 /lib/MyAPI.rb 中:

class MyAPI < Grape::API
  prefix 'api'
  version 'v1', :using => :path, :format => :json, :default_format => :json

  resource "users" do
    get do
      error!("401 invalid token", 401) unless current_user
  users = User.where('id != ?' , current_user.id) - current_user.friends
       users.as_json()
    end
  end 
end

在开发模式下,json 是正确渲染的,但是在 heroku 上,渲染的是 xml 而不是 json。

有人知道为什么吗?

非常感谢。

【问题讨论】:

  • 如果您在本地服务器上以生产模式运行应用程序,您是否有同样的问题?
  • 在我的本地服务器的生产模式下,它可以正常工作。看起来问题来自heroku。感谢您的帮助。
  • 你可以在调用 API 时粘贴你的 Heroku 实例的日志吗?
  • 信息不多2012-04-05T09:07:32 +00:00 heroku[router]: GET *****.com/api/v1/me dyno=web.1 queue=O wait=0ms service=118ms status=200 bytes=813
  • 你能在你的动作中添加一些输出吗,比如 env ?

标签: ruby-on-rails xml json heroku grape-api


【解决方案1】:

根据自述文件和代码。在 Grape 0.2.0 版本中,default_format 不是版本类方法的选项。您需要由您的班级修复它:

class MyAPI < Grape::API
  prefix 'api'
  version 'v1', :using => :path
  format :json
  default_format :json

  resource "users" do
    get do
      error!("401 invalid token", 401) unless current_user
  users = User.where('id != ?' , current_user.id) - current_user.friends
       users.as_json()
    end
  end 
end

【讨论】:

  • 我刚刚尝试了您的建议,但收到以下错误:/Users/mathieuripert/woudu/lib/MyAPI.rb:4:in format': can't convert Symbol into String (TypeError)`
  • 你用的是哪个版本的葡萄?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-10
  • 2011-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多