django - django:不返回特定端点的 cookie
问题描述
我需要从 Django 返回响应而不返回 cookie。
我正在尝试实现一个 webhook 客户端 API,它需要:
- https的使用
- 5秒内响应
- 响应中没有正文
- 响应标头中没有 cookie
- 无效 hmac 签名的 401 未授权状态码
我正在开发 Django 1.10(即将升级到 2.x),其中应用程序的其余部分受到用户通过会话验证的保护。
部分端点视图如下:
response200 = HttpResponse(status=200)
response401 = HttpResponse(status=401)
response401.close() # attempt not to set cookie
signature = request.META.get('HTTP_WEBHOOK_SIGNATURE')
if not request.method == 'POST':
return response401
if not signature:
return response401
等等。
但是,我避免使用设置会话的尝试response401.close()
不起作用。我也试过del response401['Set-Cookie']
看 Django 文档
cookieLocalTest...
仍然在这个 curl 会话中设置:
$ curl -d "param1=value1¶m2=value2" \
-H "webhook-signature: $SIGVAL" \
-H "Content-Type: application/x-www-form-urlencoded" \
-X POST http://127.0.0.1:8000/invoices/webhookendpoint \
-w "\n" -v
...
* Connected to 127.0.0.1 (127.0.0.1) port 8000 (#0)
> POST /invoices/webhookendpoint HTTP/1.1
> Host: 127.0.0.1:8000
> User-Agent: curl/7.52.1
> Accept: */*
> x-xero-signature: ZSlYlcsLbYmas53uHNrBFiVL0bLbIKetQI6x8JausfA=n
> Content-Type: application/x-www-form-urlencoded
> Content-Length: 27
>
* upload completely sent off: 27 out of 27 bytes
* HTTP 1.0, assume close after body
< HTTP/1.0 401 Unauthorized
< Date: Thu, 11 Apr 2019 08:32:50 GMT
< Server: WSGIServer/0.1 Python/2.7.13
< Vary: Cookie
< Content-Type: text/html; charset=utf-8
< Set-Cookie: LocalTest=gwx7jhsshy2qvtct1rmzv86h7xshe6ot; httponly; Path=/
<
* Curl_http_done: called premature == 0
* Closing connection 0
解决方案
看来这有效:
# ensure no cookie header is set
del request.session
response200 = HttpResponse(status=200)
response401 = HttpResponse(status=401)
...
如 curl 响应所示:
< HTTP/1.0 200 OK
< Date: Thu, 11 Apr 2019 08:49:28 GMT
< Server: WSGIServer/0.1 Python/2.7.13
< Content-Type: text/html; charset=utf-8
<
自然,如果您以登录用户的身份访问此端点,则必须再次登录。
推荐阅读
- javascript - 如何在范围外使用套接字?
- java - 运行此 Spring Batch 应用程序时发生“HikariDataSource 已关闭”异常,为什么?怎么修?
- javascript - 限制 Google 自定义搜索中的广告数量
- reactjs - 反应原生:命令失败:./gradlew app:installDebug
- c# - 如何在 GET-Method 中排除 JSON 中的一些模型字段?
- python - Python列表排序与仿真优化问题
- msbuild - 进行 WebDeploy 时不要删除 web.config
- c# - IEnumerable SelectListItem 引发 ArgumentNullException:值不能为空。(参数“来源”)
- python - 如何为 pandas 的 read_csv 选择编码类型
- node.js - Node.js GraphQL 突变定义不起作用