python cookie 的异常行为,无法设置 cookie

Weird behaviour for python cookies, unable to set cookies

我正在使用 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 调用 get 请求时,下面是我从请求中返回的内容:

任何人都可以帮助理解这种行为? 如图所示,它不是 Set-Cookie ->,而是 ->。 谢谢。

根据 the docsnameko.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