在我的rails答案中删除不必要的HTTP标头 [英] Remove unnecessary HTTP headers in my rails answers

查看:152
本文介绍了在我的rails答案中删除不必要的HTTP标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个大小很重要的API:我希望答案包含尽可能少的字节。我优化了我的JSON答案,但rails仍然响应许多奇怪的标题

  HTTP / 1.1 200 OK 
服务器:nginx /0.7.67#不是来自Rails,所以没关系。
日期:2012年4月25日星期三20:17:21 GMT#日期并不重要。我们使用ETag可以删除吗?
ETag:678ff0c6074b9456832a710a3cab8e22#需要。
Content-Type:application / json; charset = utf-8#也需要。
Transfer-Encoding:chunked#替代方案是Content-Length,所以没关系。
连接:keep-alive#好,少开TCP。
状态:200 OK#冗余!我该如何删除?
X-UA兼容:IE = Edge,chrome = 1#完全不需要。
Cache-Control:no-cache#不需要。
X-Request-Id:c468ce87bb6969541c74f6ea761bce27#根本不是真正的标题。
X-Runtime:0.001376#同样适用于这个
X-Rack-Cache:无效,传递#这个。

因此有许多不必要的HTTP标头。我可以在我的服务器(nginx)中过滤它们,但有没有办法直接在rails中停止它?

解决方案

你可以做这带有一块Rack中间件。有关在一个中执行此操作的示例,请参见 https://gist.github.com/02c1cc8ce504033d61bf 。 / p>

将其添加到您的应用配置时,请使用类似 config.middleware.insert_before(ActionDispatch :: Static,:: HeaderDelete)



您希望在运行 rake middleware ,在我的情况下是 ActionDispatch :: Static



http://guides.rubyonrails.org/rails_on_rack.html 如果你之前没有在Rails上下文中暴露过Rack,可能会有所帮助。


I am currently developing an API where size matters: I want the answer to contain as few bytes as possible. I optimized my JSON answer, but rails still responds with many strange headers

HTTP/1.1 200 OK
Server: nginx/0.7.67                            # Not from Rails, so ok.
Date: Wed, 25 Apr 2012 20:17:21 GMT             # Date does not matter. We use ETag Can I remove this?
ETag: "678ff0c6074b9456832a710a3cab8e22"        # Needed.
Content-Type: application/json; charset=utf-8   # Also needed.
Transfer-Encoding: chunked                      # The alternative would be Content-Length, so ok.
Connection: keep-alive                          # Good, less TCP overhead.
Status: 200 OK                                  # Redundant! How can I remove this?
X-UA-Compatible: IE=Edge,chrome=1               # Completely unneded.
Cache-Control: no-cache                         # Not needed.
X-Request-Id: c468ce87bb6969541c74f6ea761bce27  # Not a real header at all.
X-Runtime: 0.001376                             # Same goes for this
X-Rack-Cache: invalidate, pass                  # And this.

So there are lots of unnecessary HTTP headers. I could filter them in my server (nginx), but is there a way stop this directly in rails?

解决方案

You can do this with a piece of Rack middleware. See https://gist.github.com/02c1cc8ce504033d61bf for an example of to do it in one.

When adding it to your app config, use something like config.middleware.insert_before(ActionDispatch::Static, ::HeaderDelete)

You want to insert it before whatever the first item in the list that displays when you run rake middleware, which in my case is ActionDispatch::Static.

http://guides.rubyonrails.org/rails_on_rack.html may be somewhat helpful if you haven't been exposed to Rack in the Rails context before.

这篇关于在我的rails答案中删除不必要的HTTP标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆