我試圖定期請求服務器獲取數據。由於數據不會非常頻繁地變化,我決定添加If-None-Match
標題來優化性能。問題是即使ETag
保持不變,我仍然得到200
狀態代碼而不是304
。這是我寫的,以驗證它的一小腳本:即使在添加ETag標頭後仍未收到304響應
編輯:
import requests
headers = {'Accept':'application/json', 'Content-Type':'application/json', 'Authorization':'Bearer XXXX'}
url = "https://api.producthunt.com/v1/posts/12330"
req = None
response_data = None
for i in range(1,10):
if req:
headers = {'Accept':'application/json', 'Authorization':'Bearer XXXX', 'If-None-Match': req.headers['ETag']}
print req.request.headers.get('If-None-Match', 'Not set') # set
req = requests.get(url, headers = headers)
if response_data:
print sorted(req.json().items()) == sorted(response_data.items()) # always True
response_data = req.json()
print req.history # []
print req.status_code # always 200
- 的
ETag
頭是越來越設置。 - 返回的響應數據始終相同。
- 沒有重定向,因爲
req.history
總是返回一個空的列表。 - 我已經刪除了
Content-Type
標題,但回覆仍爲200
。 - 我使用的請求版本是
2.5.0
。
與我同樣的問題在這裏...你解決了這個問題嗎? – dofine