Rails 3 HTTP-header If-Modified-Since 使用 curl

Posted

技术标签:

【中文标题】Rails 3 HTTP-header If-Modified-Since 使用 curl【英文标题】:Rails 3 HTTP-header If-Modified-Since using curl 【发布时间】:2012-06-24 02:49:48 【问题描述】:

我有一个简单的开箱即用的 rails 服务器正在运行,我正在尝试弄清楚如何使它与 If-Modified-Since 标头一起工作。我正在使用以下 curl 请求。

curl -I localhost:3000/shows/1 --header 'If-Modified-Since: Thu, 21 Jun 2012 19:16:20 GMT'

我总是得到 200 OK,但我想要 304 Not Modified。

当我通过浏览器执行此操作时,我得到一个 304 Not Modified 但不是 curl。

我做错了吗?

更新

如果我跑了

curl -v -I localhost:3000/shows/1 --header 'If-Modified-Since: Thu, 21 Jun 2012 19:16:20 GMT'

我明白了:

* About to connect() to localhost port 3000 (#0)
*   Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 3000 (#0)
> HEAD /shows/1 HTTP/1.1
> User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5
> Host: localhost:3000
> Accept: */*
> If-Modified-Since: Thu, 21 Jun 2012 19:16:20 GMT
> 
< HTTP/1.1 200 OK 
< Content-Type: text/html; charset=utf-8
< X-Ua-Compatible: IE=Edge
< Etag: "35ec119e3b6c7ccde9eeee82afcd4ee9"
< Cache-Control: max-age=0, private, must-revalidate
< X-Request-Id: dd365eb0b80f5b9ff62bc16f3dbbe494
< X-Runtime: 0.007800
< Content-Length: 0
< Server: WEBrick/1.3.1 (Ruby/1.9.3/2012-02-16)
< Date: Thu, 21 Jun 2012 19:37:55 GMT
< Connection: Keep-Alive
< Set-Cookie: _spacebarfm-        rails_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJWY0NzNjYmFlYzJhNzgzZWRjMjc2MmU4YWFkZDc1OTIwBjsAVEkiEF9jc3JmX3Rva2VuBjsARkkiMWhjU3NvQ2VuRkR1NmJRREV1SmxOMUlaK3VhQU0ycjY2am94cFFmOTVnTTg9BjsARg%3D%3D--f9c756d08065c423cdecba2d710b1b74bf798c12; path=/; HttpOnly

的Set-Cookie:_spacebarfm-rails_session = BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJWY0NzNjYmFlYzJhNzgzZWRjMjc2MmU4YWFkZDc1OTIwBjsAVEkiEF9jc3JmX3Rva2VuBjsARkkiMWhjU3NvQ2VuRkR1NmJRREV1SmxOMUlaK3VhQU0ycjY2am94cFFmOTVnTTg9BjsARg%3D%3D - f9c756d08065c423cdecba2d710b1b74bf798c12;路径=/; HttpOnly

* Connection #0 to host localhost left intact
* Closing connection #0

控制器的rails代码是:

def index
  @shows = Show.all

  respond_to do |format|
    format.html # index.html.erb
    format.json  render json: @shows 
  end
end

Chrome 输出如下:

Request URL:http://localhost:3000/shows/1.json
Request Method:GET
Status Code:304 Not Modified
Request Headersview source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Cookie:_btest_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJTNkNzI3ODc1NjU0M2EyZTViOWY0ZTgzMjU0M2IzYmY4BjsAVEkiEF9jc3JmX3Rva2VuBjsARkkiMUFVZVN0ZHp6L0JRNGVMZDZST2JSNG54Zlg4T3VmSnk4RFNhWXRHbmljK3c9BjsARg%3D%3D--ceb783671372cbcc3ecf971340f2a2e2424c9620; _spacebarfm-rails_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJTNhMTcxNjMyNzJkMWRjYzAxY2NiN2EwN2JjMzE5YWFjBjsAVEkiEF9jc3JmX3Rva2VuBjsARkkiMTZZREp5ZC92VTlEQk9oKytCK0hKYW5na1M4bzRPRUM3U3lmWURzU09neWs9BjsARg%3D%3D--793b15b38511bd0805df3a210d7e72152905b8b9
Host:localhost:3000
If-None-Match:"bb012b3a16e7e80ec271b0d234a9b8ce"
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5
Response Headersview source
Cache-Control:max-age=0, private, must-revalidate
Connection:close
Date:Thu, 21 Jun 2012 21:09:28 GMT
Etag:"bb012b3a16e7e80ec271b0d234a9b8ce"
Server:WEBrick/1.3.1 (Ruby/1.9.3/2012-02-16)
Set-Cookie:_spacebarfm-rails_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJTNhMTcxNjMyNzJkMWRjYzAxY2NiN2EwN2JjMzE5YWFjBjsAVEkiEF9jc3JmX3Rva2VuBjsARkkiMTZZREp5ZC92VTlEQk9oKytCK0hKYW5na1M4bzRPRUM3U3lmWURzU09neWs9BjsARg%3D%3D--793b15b38511bd0805df3a210d7e72152905b8b9; path=/; HttpOnly
X-Request-Id:2435168fa20699508e9282c37566c015
X-Runtime:0.005378
X-Ua-Compatible:IE=Edge

【问题讨论】:

运行 curl-v 并粘贴输出,同时粘贴浏览器的输出(如果您使用的是 chrome,只需打开网络工具并检查请求响应)。 请发布您的 ShowsController 代码,特别是 show 操作。 【参考方案1】:

你需要使用陈旧的?方法告诉 Rails 你想使用 if-modified-then

您实际上是在调用 show 方法。将其更改为:

def show
  @show = Show.find(params[:id])

  if stale?(:etag => @show, :last_modified => @show.updated_at.utc)
    respond_to do |format|
      format.html # index.html.erb
      format.json  render json: @show 
    end
  end
end

【讨论】:

谢谢!这就是我一直在寻找的。​​span>

以上是关于Rails 3 HTTP-header If-Modified-Since 使用 curl的主要内容,如果未能解决你的问题,请参考以下文章

如果单击超链接,使浏览器提交额外的 HTTP-Header

如何在 Wordpress 中插入 Last-Modified HTTP-header?

netty系列之:在netty中处理CORS

为啥 curl 无法使用选项 -i 显示 http 标头

js实现页面刷新

Rails 3 延迟作业 - 升级到 Rails 3 会中断延迟作业任务