我如何限制我的 API 在 Rails 和 Grape 上仅接受和响应 json 格式,我已经尝试format :json
过我的 Grape 控制器并且(例如)我可以在 example.com/api/v1/ping.json 上访问它,但是我也可以通过example.com/api/v1/ping.xml、example.com/api/v1/ping.foobar访问它,扩展列表还在继续……
我想做的事情是在 example.com/api/v1/ping.not_json_extensions 上抛出错误
我正在使用:
- 导轨 (4.1.1)
- 葡萄 (0.7.0)
/config/routes.rb
mount API::Base => '/api'
/控制器/api/base.rb
module API
class Base < Grape::API
mount API::V1::Base
end
end
/controllers/api/v1/base.rb
module API
module V1
class Base < Grape::API
format :json
mount API::V1::Ping
end
end
结尾
/controllers/api/v1/ping.rb
module API
module V1
class Ping < Grape::API
include API::V1::Defaults
desc 'Returns pong.'
get :ping do
{ ping: params[:pong] || 'pong' }
end
end
end
结尾