Ad

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.

Ad

Answer

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
Ad
source: stackoverflow.com
Ad