【问题标题】:What is the and why is there a difference between request.env['CONTENT_TYPE'] and request.content_type?request.env['CONTENT_TYPE'] 和 request.content_type 之间有什么区别?
【发布时间】:2011-02-01 08:43:52
【问题描述】:

我看到request.env['CONTENT_TYPE']request.content_type 有两个不同的值。

我正在从一个单独的应用程序向我的 Rails 应用程序发送一个请求,并将 Content-Type 明确设置为 text/xml

这是我在 Rails 应用程序中看到的另一端:

request.env['CONTENT_TYPE'] = "text/xml"
request.content_type = "application/xml"

request.content_type实际上是action_dispatch.request.content_type

  1. request.env['CONTENT_TYPE']request.content_type 有什么区别?
  2. 为什么这两个值不同?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 content-type httprequest actiondispatch


    【解决方案1】:

    request.env 包含 Rack 关于内容类型的“想法”。通常,这是您提出的请求的内容类型。

    request.content_type 另一方面是 Rails 根据请求的格式对它认为的内容类型的解释。这些是在 Rails 中一个名为 mime_types.rb 的文件中定义的(我不记得是哪个部分,但你应该能够找到它),其他的可以在 config/initializers/mime_types.rb 中指定。

    【讨论】:

    • 谢谢!很好的解释。那么我是否正确地说request.content_type 对于那些未能在请求标头上设置Content-Type 的懒惰人来说更像是一种安全/便利机制?换句话说,如果我期待一个确定Content-Type,我应该检查request.env['CONTENT_TYPE']request.content_type,因为Rails会根据实际内容进行猜测以计算出此值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-08
    • 2018-12-23
    • 2010-11-07
    • 2014-07-20
    • 2011-03-01
    相关资源
    最近更新 更多