简体   繁体   English

如何为304 http响应代码设置Content-Type标头?

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

So I am using few decorators from Django to enable caching on my API: 因此,我使用了一些来自Django的装饰器来启用API上的缓存:

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

The problem is, 304 Not Modified response is coming back with text/html Content-Type header. 问题是,text / html Content-Type标头返回了304 Not Modified响应。 My API normally returns application/json Content-Type header and I would like to be consistent. 我的API通常返回application / json Content-Type标头,我希望保持一致。 Is there a way to tell Django what content type to return with 304 response code? 有没有办法告诉Django 304响应码返回哪种内容类型?

The problem is here https://github.com/django/django/blob/master/django/http/response.py#L411 问题在这里https://github.com/django/django/blob/master/django/http/response.py#L411

Write a decorator to add the mimetype again 编写装饰器以再次添加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.

相关问题 如何从 HTTP 标头响应中解析 Content-Type 的值? - How to parse the value of Content-Type from an HTTP Header Response? HTTP 响应不包含“Content-Type”标头 - HTTP Response does not contain a 'Content-Type' header 金字塔:是否设置Content-Type以匹配匹配的header? - Pyramid: Set Content-Type in response to match accept header? Python HTTP标头内容类型边界 - Python HTTP Header Content-Type boundary 在Tornado中未设置Content-Type标头 - Content-Type header not getting set in Tornado 如何使用django.test.Client发出没有Content-Type标头的HTTP请求? - How can I make an HTTP request with no Content-Type header using django.test.Client? 测试期间,“ DRF响应”内容类型设置为“无” - DRF Response content-type set to None during tests 使用python的http服务器时如何设置content-type? - How do I set the content-type when using python's http server? 如何将请求和响应“Content-Type”设置为“application/json;charset=UTF-8”? - How to set Request and response “Content-Type” to “application/json;charset=UTF-8”? 如何检测和更正 python 中电子邮件标题中的 Content-Type 字符集? - How to detect and correct the Content-Type charset in email header in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM