我如何限制我的API接受&只在Rails上響應json格式&葡萄,我試過format :json
在我的葡萄控制器上(例如)我可以在example.com/api/v1/ping.json上訪問它,但我也可以通過example.com/api/v1/ping.xml,example.com/api/v1/ping.foobar訪問它,並且擴展名列表繼續...如何強制Grape接受並返回僅JSON?
我會喜歡做的是,在example.com/api/v1/ping.not_json_extensions拋出錯誤
即時通訊使用:
- 軌(4.1.1)
- 葡萄(0.7.0)
/config/routes.rb
mount API::Base => '/api'
/controllers/api/base.rb
module API
class Base < Grape::API
mount API::V1::Base
end
end
/控制器/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
末
我看到內存泄漏,因爲攻擊者可能用大量不支持的格式向您發送垃圾郵件。因此這會不斷增加符號緩存。所以這基本上是一個內存和安全修復。對我的修復將是一個其他的投擲! – Nick
事實上,它變得稍微複雜一點,因爲我改變了許多規格。看着它。 – Nick
西門,我將提交一個補丁, https://github.com/realtymaps/grape/blob/v.0.9.0_format_patch/lib/grape/middleware/formatter.rb#L95-L113 https://開頭github.com/realtymaps/grape/commit/ecc245a2a2110d5051893793228cd4053929135c – Nick