简体   繁体   English

如何从 Django 响应中删除 Set-Cookie header

[英]How to remove Set-Cookie header from Django response

I've created a middleware but there doesn't seem to be a Set-Cookie header in the response?我创建了一个中间件,但响应中似乎没有 Set-Cookie header? But when testing the responses, its definitely there但是在测试响应时,它肯定存在

MIDDLEWARE = (
    "apps.base.middleware.RemoveHeadersMiddleware",
    ###
)

class RemoveHeadersMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response
        # One-time configuration and initialization.

    def __call__(self, request):
        response = self.get_response(request)
        # No Set-Cookie header here?????
        # del response._headers['Set-Cookie']
        return response

The Set-Cookie header is somewhat magic, it is generated by Django based on the HTTPResponse's cookie attribute. Set-Cookie header有点神奇,它是Django根据HTTPResponse的cookie属性生成的。 You can remove the Set-Cookie header by clearing response.cookie in a middleware that runs after any cookie-writing middlewares you may have.您可以通过在您可能拥有的任何编写 cookie 的中间件之后运行的中间件中清除response.cookie来删除 Set-Cookie header。 You probably don't want to do this for every request, but it can be useful for certain endpoints that may be highly cacheable.您可能不想对每个请求都执行此操作,但它对于某些可能高度可缓存的端点很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django:在视图中,我如何获取将成为以下响应的Set-Cookie标头一部分的sessionid? - Django : In a view how do I obtain the sessionid which will be part of the Set-Cookie header of following response? 如何在HTTP响应中处理多个Set-Cookie标头 - How to handle multiple Set-Cookie header in HTTP response Python请求:在响应标题中从Set-Cookie设置cookie - Python Requests: Set a cookie from Set-Cookie in Response Headers 为什么sessionid是Django上的空字符串返回响应Set-Cookie? - Why sessionid is empty string return response Set-Cookie on django? 如何在 django 2.0.13 版本中设置 set-cookie 的 SameSite 属性? - How to set the SameSite attribute of set-cookie in django version 2.0.13? 使用请求模块,如何处理请求响应中的“set-cookie”? - Using requests module, how to handle 'set-cookie' in request response? Python解析set-cookie头 - Python parsing set-cookie header Flask-Login login_user() 显示来自 Set-Cookie 标头的加密 cookie - Flask-Login login_user() show encrypted cookie from Set-Cookie header python web-client multipile set-cookie标头,获取原始set-cookie标头 - python web-client multipile set-cookie header, get raw set-cookie header 为什么在Set-Cookie标头字段中找不到CSRF令牌? - why CSRF token is not found in Set-Cookie header field?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM