how to renew a http cookie

1,601 views
Skip to first unread message

Rajanikanth Jammalamadaka

unread,
Mar 12, 2014, 1:12:08 PM3/12/14
to golan...@googlegroups.com
Hi

I read somewhere that in go, it is recommended to create a http client only once.

I have a situation in which I create a client with cookie (by using a https Url) but the cookie expires after sometime.

What is the best way to renew the cookie other than to create a separate client for every http request?

Here is the snippet of code:

http://play.golang.org/p/yUOTqGmJvV

Thanks,
Raj

Jason Del Ponte

unread,
Mar 12, 2014, 1:45:56 PM3/12/14
to golan...@googlegroups.com
Is it valid for the client to just change the expiration date on the cookie from the server's response, or remove the expiration? This is a bit hair since the server may reset the cookie's expiration in its response to the clients request.  (does this make sense, I'm not sure if i'm reading your question correctly)

Once you create the client's cookie jar you should be able to modify that cookie jar as needed.  Though make sure modification of the cookie jar vai SetCookie method is protected with the requests, because the default Cookie is not thread safe.

Cheers,
Jason

Rajanikanth Jammalamadaka

unread,
Mar 12, 2014, 1:59:03 PM3/12/14
to golan...@googlegroups.com
Thanks for your reply. I want to get a fresh cookie from the server as soon as the current one expires. Was wondering if there is any other way to obtain it without creating a new client?

Thanks,
Raj

Jason Del Ponte

unread,
Mar 12, 2014, 2:25:28 PM3/12/14
to golan...@googlegroups.com
Your code should be able to continually reuse the client for multiple requests, and should not need to create a new instances of client ever unless you want multiple clients. http://golang.org/pkg/net/http/#Client states the client can be used for multiple requests, and concurrently.  When the cookie does expire your code will need to re-perform the task which provided the client with the cookie in the first place.  In the example given i'm assuming this is the user/pass client.PostForm.

Cheers,
Jason

Rajanikanth Jammalamadaka

unread,
Mar 12, 2014, 2:30:48 PM3/12/14
to golan...@googlegroups.com
ok, thanks for your help.

Raj

Matt Silverlock

unread,
Mar 13, 2014, 6:27:32 AM3/13/14
to golan...@googlegroups.com
Consider taking a look at http://www.gorillatoolkit.org/pkg/sessions – which makes it fairly trivial to set session cookie expirations.

Rajanikanth Jammalamadaka

unread,
Mar 16, 2014, 4:13:17 PM3/16/14
to golan...@googlegroups.com
Thanks Matt.

Rajanikanth Jammalamadaka

unread,
Mar 18, 2014, 3:31:16 PM3/18/14
to golan...@googlegroups.com
Hi Matt,

Can you show a simple example of how the sessions package can be applied to my case: http://play.golang.org/p/yUOTqGmJvV

Thanks,
Raj




On Thursday, March 13, 2014 6:27:32 AM UTC-4, Matt Silverlock wrote:
Reply all
Reply to author
Forward
0 new messages