python - python cookie的奇怪行为,无法设置cookie
问题描述
我使用 Python Nameko 作为我的微服务框架,当我尝试在我的 get 请求中设置 cookie 时,我似乎做不到,下面是我的代码:
from http import cookies
from nameko.web.handlers import http
@http('GET', '/hello')
def say_hello(self, request):
c = cookies.SimpleCookie()
c['test-cookie'] = 'test-1'
return 200, c, 'Hello World!'
当我使用 Postman 调用获取请求时,以下是我从请求中返回的内容:
任何人都可以帮助理解这种行为吗?如图所示,它不是 Set-Cookie ->,而是 ->。谢谢你。
解决方案
根据文档,三元组响应类型nameko.http
为(status_code, headers dict, response body)
。也就是第二个参数是headers的dict,和cookie对象不一样
要设置 cookie,您需要构建werkzeug.wrappers.Response
自己的实例(也包含在文档中的该列表中):
@http('GET', '/hello')
def say_hello(self, request):
response = Response("Hello World!")
response.set_cookie('test-cookie', 'test-1')
return response
推荐阅读
- c - 为什么在使用 wprintf 时 ©(版权符号)被替换为 (C)?
- angular - Cousera 上的 Ionic 和 Angular
- amp-ad - 如何将 amp-auto-ad 配置为使用宽度 100% 而不是 100vw
- php - Symfony 4 和 Amazon S3 - AWS 凭证不起作用
- css - 移动视图上的内容被截断(iphone x、ipad、ipad pro)
- linux - 如何使用 bash 将文件中每一行的前 2 个字母添加到列表中?
- rust - 如何导入substrate_primitives 以使用sr25519?
- python - Django QuerySet 查找返回多个对象
- android - libGDX 看不到 Android SDK
- sql-server - 覆盖索引包括rowversion?是好是坏