[英]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.