You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was looking for a way to temporary disable cookie persistence.
When I use requests to access an URL cookies are automatically sent back to the server (in the following example the requested URL set some cookie values and then redirect to another URL that display the stored cookie)
Is it possible to temporary disable cookie handling in the same way you set Chrome or Firefox to not accept cookies?
I made this question on SO and it seems that the only way to go is patching the requests.cookies.RequestsCookieJar.update. I think that should be an easier way to do this in requests, Maybe passing parameter to the get method?
The text was updated successfully, but these errors were encountered:
By design Requests' primary API supports the 90% use-case, which involves persisting cookies. We are very unlikely to add a parameter to the main methods to allow you not to persist cookies.
If you absolutely don't want to, my recommendation is that you use a session along with a subclass of the RequestsCookieJar. I haven't tried the following, but it ought to work:
importrequestsfromrequests.cookiesimportRequestsCookieJarclassBlackHoleCookieJar(RequestsCookieJar):
""" This class's attitude to cookies can be described in three words: nom nom nom. """defset(self, name, value, **kwargs):
passdefupdate(self, other):
passs=requests.Session()
s.cookies=BlackHoleCookieJar()
Subsequent requests through that session should throw cookies away. Bear in mind that the BlackHoleCookieJar above is for example purposes only: in real code there are a few other methods you'd want to override, like .copy().
Thank you very much for the explanation and the sample code. At first I thought that cookie persistence were available only for sessions. I'll go with the BlackHoleCookieJar.
I was looking for a way to temporary disable cookie persistence.
When I use requests to access an URL cookies are automatically sent back to the server (in the following example the requested URL set some cookie values and then redirect to another URL that display the stored cookie)
Is it possible to temporary disable cookie handling in the same way you set Chrome or Firefox to not accept cookies?
I made this question on SO and it seems that the only way to go is patching the
requests.cookies.RequestsCookieJar.update
. I think that should be an easier way to do this in requests, Maybe passing parameter to theget
method?The text was updated successfully, but these errors were encountered: