Rails 3 中的 HEAD HTTP 请求 [英] HEAD HTTP requests in Rails 3

查看:52
本文介绍了Rails 3 中的 HEAD HTTP 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Rails 3 当前将 HEAD 请求路由到匹配的 GET 路由.有头吗?请求上的方法,但 in 返回 false 并且请求的行为类似于 get 请求.我可以检测请求是否是 HEAD 请求吗?

Rails 3 currently routes a HEAD request to the matching GET route. There is a head? method on the request, but in returns false and the request acts like a get request. Can I detect if the request is a HEAD request?

推理:我知道 HEAD 请求应该返回与 get 完全相同的标头,因此 Rails 想要执行完整的 GET,然后剃掉正文.但是,我可以遵守此请求,而无需发出与 GET 相同的数据库调用等.这有意义吗?

Reasoning: I get that a HEAD request should return the EXACT same headers as the get, so Rails wants to perform the full GET and then shave off the body. However, I can conform to this request without issuing the same DB calls, etc that the GET would. Does this make sense?

推荐答案

我确实遇到了这个问题.事实证明,启用缓存会导致这种情况.在您的环境和#head 中关闭缓存?将按预期工作.

I had this exact issue. It turns out that enabling caching causes this. Turn caching off in your environment and #head? will work as expected.

问题在于 Rack::Cache 将 HEAD 请求转换为 GET 请求,以便它们可以被缓存.这可以说是正确的行为,但它干扰了我的应用程序.

The issue is that Rack::Cache turns HEAD requests into GET requests so they can be cached. This is arguably the correct behavior, but it interfered with my application.

这篇关于Rails 3 中的 HEAD HTTP 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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