0

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.

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
Joe. L
  • 406
  • 2
  • 14

1 Answers1

1

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
second
  • 28,029
  • 7
  • 75
  • 76
  • Thanks for the answer @second, I tried to use the wrapper you suggested, but I only get this: Error: TypeError: Payload must be a string. Got `None`, when I use the wrapper with .set_cookie function. – Joe. L Apr 15 '19 at 05:24
  • sounds like it might be something specific to what you are doing. maybe post a new question with a _runnable_ (but minimal) code example that shows the issue – second Apr 15 '19 at 12:14
  • Hi second, it's all fine when I do response = Response("Hello World!", 200), followed by set cookie. Felt like it's a bug or something – Joe. L Apr 17 '19 at 07:12