Search code examples
pythoncookieshttp-headerssetcookienameko

Weird behaviour for python cookies, unable to set cookies


I am using Python Nameko as my microservice framework, and when I try to set cookies in my get request, I can't seems to do it, below is my code:

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!'

When I call the get request using Postman, below is what I get back from the request: enter image description here

Anyone can help in understanding the behaviour? Instead of Set-Cookie ->, it's ->, as shown in the image. Thank you.


Solution

  • As per the docs, the 3-tuple response type for nameko.http is (status_code, headers dict, response body). That is, the second argument is a dict of headers, which is not the same as a cookie object

    To set cookies you need to construct an instance of werkzeug.wrappers.Response yourself (also included in that list in the docs):

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