【发布时间】:2011-06-05 15:37:10
【问题描述】:
在 Sinatra 中是否可以将 content_type 'application/json' 设为默认值?因为我正在构建一个 REST API。
【问题讨论】:
标签: json ruby rest sinatra content-type
在 Sinatra 中是否可以将 content_type 'application/json' 设为默认值?因为我正在构建一个 REST API。
【问题讨论】:
标签: json ruby rest sinatra content-type
当然,将content_type 添加到before 回调中:
class MyApp < Sinatra::Base
before do
content_type 'application/json'
end
...
end
Sinatra 1.1 在过滤器之前引入模式匹配:
before '/admin/*' do
check_logged_in
end
【讨论】:
对于 JSON API,为所有响应设置默认 Content-Type 的最推荐方法是在 Sinatra 类中添加以下内容:
set :default_content_type, :json
它将在您的所有回复中包含Content-Type: application/json 标头。
【讨论】:
set :default_content_type, :json