Python cookies的奇怪行为,无法设置cookies

2024-05-29 02:49:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用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请求时,下面是我从请求中得到的结果: enter image description here

有人能帮助理解这种行为吗? 它不是Set Cookie->;,而是->;,如图所示。 非常感谢。你知道吗


Tags: 代码fromtestimportgt框架webhttp
1条回答
网友
1楼 · 发布于 2024-05-29 02:49:05

根据the docsnameko.http的三元组响应类型是(status_code, headers dict, response body)。也就是说,第二个参数是头的dict,它与cookie对象不同

要设置cookie,您需要自己构造一个^{}实例(也包括在文档的列表中):

    @http('GET', '/hello')
    def say_hello(self, request):
        response = Response("Hello World!")
        response.set_cookie('test-cookie', 'test-1')
        return response

相关问题 更多 >

    热门问题