librelist archives

« back to archive

setting cookie problem?

setting cookie problem?

From:
kevin
Date:
2011-08-14 @ 22:04
this does not work, no cookie is set. anything wrong with this?

def before_request():
    r = flask.make_response()
    r.set_cookie('test',value='test')

Re: [flask] setting cookie problem?

From:
Armin Ronacher
Date:
2011-08-14 @ 23:05
Hi,

On 8/15/11 12:04 AM, kevin wrote:
> this does not work, no cookie is set. anything wrong with this?
> 
> def before_request():
>     r = flask.make_response()
>     r.set_cookie('test',value='test')
Responses are passed through.  Since you don't know the response until
it comes from the view you need to do that in the after_request function:

@app.after_request
def after_request(response):
    response.set_cookie('test', value='test')
    return response


Regards,
Armin

Re: [flask] setting cookie problem?

From:
kevin
Date:
2011-08-14 @ 23:16
On Sun, Aug 14, 2011 at 4:05 PM, Armin Ronacher <armin.ronacher@active-4.com
> wrote:

> Hi,
>
> On 8/15/11 12:04 AM, kevin wrote:
> > this does not work, no cookie is set. anything wrong with this?
> >
> > def before_request():
> >     r = flask.make_response()
> >     r.set_cookie('test',value='test')
> Responses are passed through.  Since you don't know the response until
> it comes from the view you need to do that in the after_request function:
>
> so please correct me if i am wrong. after_request is the only place one can
set cookie when using flask? not in before_request or in the url handler?

Re: [flask] setting cookie problem?

From:
Armin Ronacher
Date:
2011-08-14 @ 23:26
Hi,

On 8/15/11 1:16 AM, kevin wrote:
> so please correct me if i am wrong. after_request is the only place one
> can set cookie when using flask? not in before_request or in the url
> handler?
Correct.  However there are many ways you can still make it works.  For
example this snippet shows how you can build per-request after request
handlers: http://flask.pocoo.org/snippets/53/

With that you can issue a deferred call in a before_request handler:

@app.before_request
def before_request():
    @after_this_request
    def attach_cookie(response):
        response.set_cookie('foo', 'bar')
        return response


Regards,
Armin

Re: [flask] setting cookie problem?

From:
kevin
Date:
2011-08-14 @ 23:43
On Sun, Aug 14, 2011 at 4:26 PM, Armin Ronacher <armin.ronacher@active-4.com
> wrote:

> Hi,
>
> On 8/15/11 1:16 AM, kevin wrote:
> > so please correct me if i am wrong. after_request is the only place one
> > can set cookie when using flask? not in before_request or in the url
> > handler?
> Correct.

OK, is this also true for response.headers.add? ie can i add headers only
from after_request? thanks

Re: [flask] setting cookie problem?

From:
Jay Baker
Date:
2011-08-15 @ 20:01
I *think* Armin may have misread your 2nd message Kevin. You can in fact set
cookies in the url handler if that works better for you.

You can headers in after_request or url handlers. It is like this, if you
have the response object ... and it is not going to be overwritten ... then
you can set headers and cookies.

On Sun, Aug 14, 2011 at 6:43 PM, kevin <kevincastiglione@gmail.com> wrote:

> On Sun, Aug 14, 2011 at 4:26 PM, Armin Ronacher <
> armin.ronacher@active-4.com> wrote:
>
>> Hi,
>>
>> On 8/15/11 1:16 AM, kevin wrote:
>> > so please correct me if i am wrong. after_request is the only place one
>> > can set cookie when using flask? not in before_request or in the url
>> > handler?
>> Correct.
>
> OK, is this also true for response.headers.add? ie can i add headers only
> from after_request? thanks
>

Re: [flask] setting cookie problem?

From:
kevin
Date:
2011-08-16 @ 01:00
On Mon, Aug 15, 2011 at 1:01 PM, Jay Baker <jbaker.work@gmail.com> wrote:

> I *think* Armin may have misread your 2nd message Kevin. You can in fact
> set cookies in the url handler if that works better for you.
>
> You can headers in after_request or url handlers. It is like this, if you
> have the response object ... and it is not going to be overwritten ... then
> you can set headers and cookies.
>
can you please give an example of how to set cookie and add header in url a
url handler?
thanks!

Re: [flask] setting cookie problem?

From:
Jay Baker
Date:
2011-08-16 @ 01:09
You just use make_response to get a response object and then set the same
way as in the other examples. Let me know if you need more detail.

- sent from my android phone
On Aug 15, 2011 8:02 PM, "kevin" <kevincastiglione@gmail.com> wrote:
> On Mon, Aug 15, 2011 at 1:01 PM, Jay Baker <jbaker.work@gmail.com> wrote:
>
>> I *think* Armin may have misread your 2nd message Kevin. You can in fact
>> set cookies in the url handler if that works better for you.
>>
>> You can headers in after_request or url handlers. It is like this, if you
>> have the response object ... and it is not going to be overwritten ...
then
>> you can set headers and cookies.
>>
> can you please give an example of how to set cookie and add header in url
a
> url handler?
> thanks!

Re: [flask] setting cookie problem?

From:
kevin
Date:
2011-08-16 @ 02:55
so that is what i did in before_request and it did not work. but that would
work in app handler?



this does not work, no cookie is set. anything wrong with this?

def before_request():
    r = flask.make_response()
    r.set_cookie('test',value='test')

On Mon, Aug 15, 2011 at 6:09 PM, Jay Baker <jbaker.work@gmail.com> wrote:

> You just use make_response to get a response object and then set the same
> way as in the other examples. Let me know if you need more detail.
>
> - sent from my android phone
> On Aug 15, 2011 8:02 PM, "kevin" <kevincastiglione@gmail.com> wrote:
> > On Mon, Aug 15, 2011 at 1:01 PM, Jay Baker <jbaker.work@gmail.com>
> wrote:
> >
> >> I *think* Armin may have misread your 2nd message Kevin. You can in fact
> >> set cookies in the url handler if that works better for you.
> >>
> >> You can headers in after_request or url handlers. It is like this, if
> you
> >> have the response object ... and it is not going to be overwritten ...
> then
> >> you can set headers and cookies.
> >>
> > can you please give an example of how to set cookie and add header in url
> a
> > url handler?
> > thanks!
>

Re: [flask] setting cookie problem?

From:
Jay Baker
Date:
2011-08-16 @ 03:18
If you look at what you have below, ask yourself, what is that response
object? What happens to it when the url handler gets called? In
before_request, you have no valid response. It is "setting up" the request.

Anyway, the easiest way to investigate that is to just try it and look at
the results.
I suggest looking at  the source of render_template or whatever else you may
be using in the url handler to further understand the lifestyle of.the
response object.

- sent from my android phone
On Aug 15, 2011 9:56 PM, "kevin" <kevincastiglione@gmail.com> wrote:
> so that is what i did in before_request and it did not work. but that
would
> work in app handler?
>
>
>
> this does not work, no cookie is set. anything wrong with this?
>
> def before_request():
> r = flask.make_response()
> r.set_cookie('test',value='test')
>
> On Mon, Aug 15, 2011 at 6:09 PM, Jay Baker <jbaker.work@gmail.com> wrote:
>
>> You just use make_response to get a response object and then set the same
>> way as in the other examples. Let me know if you need more detail.
>>
>> - sent from my android phone
>> On Aug 15, 2011 8:02 PM, "kevin" <kevincastiglione@gmail.com> wrote:
>> > On Mon, Aug 15, 2011 at 1:01 PM, Jay Baker <jbaker.work@gmail.com>
>> wrote:
>> >
>> >> I *think* Armin may have misread your 2nd message Kevin. You can in
fact
>> >> set cookies in the url handler if that works better for you.
>> >>
>> >> You can headers in after_request or url handlers. It is like this, if
>> you
>> >> have the response object ... and it is not going to be overwritten ...
>> then
>> >> you can set headers and cookies.
>> >>
>> > can you please give an example of how to set cookie and add header in
url
>> a
>> > url handler?
>> > thanks!
>>

Re: [flask] setting cookie problem?

From:
DasIch
Date:
2011-08-14 @ 22:18
You are not returning the response you created in the view, that
response is never send to the client and therefore no cookie is set.