44
votes

In this way, I want to set my cookie. But it fails to set.

@app.route('/')
def index():
    res = flask.make_response()
    res.set_cookie("name", value="I am cookie")

When I print res it shows <Response 0 bytes [200 OK] But not set cookie

3
You might not be aware how cookies actually work: You instruct the browser to set it in one request and in the next request from the same browser the cookies will be set. You code is not sufficient to test that. - Klaus D.
@KlausD. I do not think it's completely fair to say that : once the server responded with a cookie it means that the response is definitely not empty (0 bytes). Yes, the data is in the Set-Cookie header and the Payload is zero but a more verbose res(ponse) variable dump should detail that. - Alex
@Alex Please not that a) the question is nearly 3 years old and b) has been substantially edited after my comment. - Klaus D.
@KlausD. I think this answer might become a reference for people coming from other stacks (ex: PHP or Java) and try to learn Flask - as is my case. - Alex

3 Answers

58
votes

You have return the response after set the cookie.

@app.route('/')
def index():
    resp = make_response(render_template(...))
    resp.set_cookie('somecookiename', 'I am cookie')
    return resp 

This way cookie will generate in your browser but you can get this cookie in next request.

@app.route('/get-cookie/')
def get_cookie():
    username = request.cookies.get('somecookiename')
2
votes

The cookie you set will be visible if you either use other tools to see it (ex: Press F12 for Developer Tools in Firefox or Chrome) or use a piece of code of JavaScript inside the rendered response.

The cookies are set on the browser by either browser itself (JavaScript) or as a response from a server.

The difference is of great importance as even if the cookie is set by the server the cookie might not be set on the browser (ex: cases where cookie are completely disabled or discarded).

So even if the server might tell "I set up the cookie" - the cookie might not be present on the browser.

For the server to be sure that the cookie was set a subsequent request from the browser is needed (with the cookie present in the request's headers).

So even if the Flask's response (res variable) will mention that the cookie is set we can only be sure that it was set by the server but it will have no confirmation about it from the browser.

Advanced Another aspect is about how Flask or any other API is creating the responses. As the payload (html/template code) and headers (cookie) are set at same time - the "code" inside the payload (html/template code) might not have access to the cookie. So you might not be able to set a cookie and display it in the response.

An idea might be to (be able to) first set the cookies and THEN to render the context and the order of setup to be important - so that the html/template to be aware of already setup values. But even in this case is only the server's confirmation that it set up the cookie.

A solution

@app.route('/')
def index():
    res = flask.make_response()
    res.set_cookie("name", value="I am cookie")

    # redirect to a page that display the cookie
    resp.headers['location'] = url_for('showcookies') 

    return resp, 302
0
votes

This response will set cookie in you browser

def func():
    response = make_response( render_template() )
    response.set_cookie( "name", "value" )
    return response