I couldn't find any documentations regarding cookies modification in the official website, i.e. no api doc for requests.cookies.RequestsCookieJar
.
For example,
session = requests.Session()
a = session.head('http://www.google.co.uk')
session.cookies
<[Cookie(version=0, name='NID', value='67=CXdvwjj9sjd-13Y0VyRQyUs8PxXaxyMhiGrrozXP7RWSjf-5alV4D17ORcfnZNYLAmlHXSVlHuS5LcuE4-v6vnzRQS-Gt72hgbGye0apoBoW5KJeVXA2o2E0gE-8jIeY', port=None, port_specified=False, domain='.google.co.uk', domain_specified=True, domain_initial_dot=True, path='/', path_specified=True, secure=False, expires=1424443599, discard=False, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False), Cookie(version=0, name='PREF', value='ID=41c5d5cac7d22262:FF=0:TM=1408632399:LM=1408632399:S=wTfY_LkkZnSsBxoL', port=None, port_specified=False, domain='.google.co.uk', domain_specified=True, domain_initial_dot=True, path='/', path_specified=True, secure=False, expires=1471704399, discard=False, comment=None, comment_url=None, rest={}, rfc2109=False)]>
Now I want to change value of 'NID'
If I do session.cookies['NID'] = 'abc'
, it would ended up with duplicated keys like the following:
<[Cookie(version=0, name='NID', value='abc', port=None, port_specified=False, domain='', domain_specified=False, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False), Cookie(version=0, name='NID', value='67=CXdvwjj9sjd-13Y0VyRQyUs8PxXaxyMhiGrrozXP7RWSjf-5alV4D17ORcfnZNYLAmlHXSVlHuS5LcuE4-v6vnzRQS-Gt72hgbGye0apoBoW5KJeVXA2o2E0gE-8jIeY', port=None, port_specified=False, domain='.google.co.uk', domain_specified=True, domain_initial_dot=True, path='/', path_specified=True, secure=False, expires=1424443599, discard=False, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False), Cookie(version=0, name='PREF', value='ID=41c5d5cac7d22262:FF=0:TM=1408632399:LM=1408632399:S=wTfY_LkkZnSsBxoL', port=None, port_specified=False, domain='.google.co.uk', domain_specified=True, domain_initial_dot=True, path='/', path_specified=True, secure=False, expires=1471704399, discard=False, comment=None, comment_url=None, rest={}, rfc2109=False)]>
My current approach is to do session.cookies['NID'] = None
first, this removes the key/value, and then session.cookies['NID'] = 'abc'
This sometimes works, but it completely ignores the cookies attributes.
What is a proper way of doing it?
To add cookies to a request for authentication, use the header object that is passed to the get/sendRequest functions. Only the cookie name and value should be set this way. The other pieces of the cookie (domain, path, and so on) are set automatically based on the URL the request is made against.
Cookies are sent with every request, so they can worsen performance (especially for mobile data connections). Modern APIs for client storage are the Web Storage API ( localStorage and sessionStorage ) and IndexedDB.
How do I send a request with Cookies? [Python Code] To send a request with a Cookie, you need to add the "Cookie: name=value" header to your request. To send multiple cookies in a single Cookie header, separate them with semicolons or add multiple "Cookie: name=value" request headers.
For me worked:
for cookie in response.cookies:
if cookie.name == 'NID':
cookie.value = 'abc'
break
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With