gpt4 book ai didi

ruby-on-rails - Rails 3 HTTP-header If-Modified-Since 使用 curl

转载 作者:可可西里 更新时间:2023-11-01 16:56:58 25 4
gpt4 key购买 nike

我有一个开箱即用的简单 Rails 服务器正在运行,我正在尝试弄清楚如何让它与 If-Modified-Since header 一起工作。我正在使用以下 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;路径=/;仅限HTTP

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

Controller 的 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

最佳答案

你需要用陈旧的吗?告诉 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

关于ruby-on-rails - Rails 3 HTTP-header If-Modified-Since 使用 curl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11145447/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com