Requests: how can I pass gmail's cookie to requests session?

Created on 17 Oct 2016  路  1Comment  路  Source: psf/requests

Hello
assume that i define a session
r = requests.Session
and i want to set cookie for it :
r.cookies = my_cookies
I know that I must use cookie jar . and I know taht I must use dictionary format. But I have a problem!
normally cookies are in this format
ukey=*****; __qca=*****; _ga=******;
and they are convertable to doctionary format easily
{'ukey' : '****' , '__qca' : '****' , '__ga' : '*****' }
but how can deal whith gmail's cookie ? they have diffrent format , for example
S=gmail=****; COMPASS=gmail=****;
I tested SimpleCookie , that some one requested in this answer at stackoverflow.com
http://stackoverflow.com/questions/32281041/converting-cookie-string-into-python-dict
but I got this error message
File "/usr/lib/python3.5/site-packages/requests/sessions.py", line 510, in head return self.request('HEAD', url, **kwargs) File "/usr/lib/python3.5/site-packages/requests/sessions.py", line 475, in request resp = self.send(prep, **send_kwargs) File "/usr/lib/python3.5/site-packages/requests/sessions.py", line 611, in send extract_cookies_to_jar(self.cookies, request, r.raw) File "/usr/lib/python3.5/site-packages/requests/cookies.py", line 133, in extract_cookies_to_jar jar.extract_cookies(res, req) AttributeError: 'dict' object has no attribute 'extract_cookies'
Please guid me

Most helpful comment

Hey @alireza-amirsamimi, thanks for opening this ticket. Requests currently doesn't support passing a dict object to Session's cookies attribute. You must first convert your dictionary into a CookieJar-like object before passing it to the session. I've attached the code below that you'll need to solve your issue. In the future, it's preferred that questions regarding how to use Requests are opened on Stackoverflow instead of the Github ticket tracker. Thanks again!

e.g.

from http.cookies import SimpleCookie
from requests.cookies import cookiejar_from_dict
from requests import Session

r = Session()
my_cookie = SimpleCookie()
my_cookie.load('rawcookie=string;')

cookies = {key: morsel.value for key, morsel in my_cookie.items()}

r.cookies = cookiejar_from_dict(cookies)

Just to make note of it, this is an occurrence of #3595.

>All comments

Hey @alireza-amirsamimi, thanks for opening this ticket. Requests currently doesn't support passing a dict object to Session's cookies attribute. You must first convert your dictionary into a CookieJar-like object before passing it to the session. I've attached the code below that you'll need to solve your issue. In the future, it's preferred that questions regarding how to use Requests are opened on Stackoverflow instead of the Github ticket tracker. Thanks again!

e.g.

from http.cookies import SimpleCookie
from requests.cookies import cookiejar_from_dict
from requests import Session

r = Session()
my_cookie = SimpleCookie()
my_cookie.load('rawcookie=string;')

cookies = {key: morsel.value for key, morsel in my_cookie.items()}

r.cookies = cookiejar_from_dict(cookies)

Just to make note of it, this is an occurrence of #3595.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ReimarBauer picture ReimarBauer  路  4Comments

jakul picture jakul  路  3Comments

iLaus picture iLaus  路  3Comments

JimHokanson picture JimHokanson  路  3Comments

eromoe picture eromoe  路  3Comments