json 为 Sinatra 设置默认 content_type

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4632122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:42:33  来源:igfitidea点击:

Set default content_type for Sinatra

rubyjsonsinatracontent-type

提问by ma11hew28

In Sinatra, is it possible to make content_type 'application/json'the default? cause I'm writing an api.

在 Sinatra 中,是否可以content_type 'application/json'设置默认值?因为我正在写一个api。

回答by Adam Lassek

Sure, add content_typeto the beforecallback:

当然,添加content_typebefore回调:

class MyApp < Sinatra::Base

  before do
    content_type 'application/json'
  end

  ...

end

Sinatra 1.1introduces pattern-matching before filters:

Sinatra 1.1在过滤器之前引入了模式匹配:

before '/admin/*' do
  check_logged_in
end