簡體   English   中英

如何為304 http響應代碼設置Content-Type標頭?

[英]How to set Content-Type header for 304 http response code?

因此,我使用了一些來自Django的裝飾器來啟用API上的緩存:

@require_GET
@cache_page(100)
@cache_control(max_age=100, s_maxage=100)
@csrf_exempt
def my_api(request):

問題是,text / html Content-Type標頭返回了304 Not Modified響應。 我的API通常返回application / json Content-Type標頭,我希望保持一致。 有沒有辦法告訴Django 304響應碼返回哪種內容類型?

問題在這里https://github.com/django/django/blob/master/django/http/response.py#L411

編寫裝飾器以再次添加mimetype

def RestoreMime(fn):
  def Wrapper(*args, **kwds):
    response = fn(*args, **kwds)
    response['Content-type'] = your_mime_type
    return response
  return Wrapper

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM