一段很简单的代码 (rails 6)
class HomeController < ApplicationController
def index
@subjects = Subject.all
if stale? @subjects
render plain: 'stale', status: 200
end
end
end
执行curl -i 'http://127.0.0.1:3000/' -H 'If-None-Match: W/"bfd6f8bb9d7c5c210ab13b06a5cfcde1"' -H 'If-Modified-Since: Thu, 02 Apr 2020 08:01:57 GMT' --compressed
由于 if-modified-since 时间早于@subjects中最大的 updated_at, 所以会执行render plain: 'stale', status: 200
,IDE 中看到日志也是Completed 200 OK in 15ms
,断点执行 response.status 返回也是 200,但 curl 中的响应结果却是 304,如下
HTTP/1.1 304 Not Modified
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Download-Options: noopen
X-Permitted-Cross-Domain-Policies: none
Referrer-Policy: strict-origin-when-cross-origin
ETag: W/"bfd6f8bb9d7c5c210ab13b06a5cfcde1"
Last-Modified: Fri, 03 Apr 2020 02:06:29 GMT
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: a42b9511-1df3-490b-8230-f6400fa92d1b
X-Runtime: 0.515872
使用 charlse 抓包确实是 304。
但如果把render plain: 'stale', status: 200
中的 status 改为 201 或其他值,同样的 curl 请求返回的结果就是设置的值。
想不通。求助大佬们