aze
aze

Reputation: 850

Python Request What is the proper way to modify the value of an existing cookie?

I am accessing a webpage which creates a cookie with a value, and then modify this value and access another page from the same website. Using librequests in python I got the following cookie: s is a session opened with s = requests.Session()

In [63]: s.cookies
Out[63]: <RequestsCookieJar[Cookie(version=0, name='my_cookie', value='normal_value', port=None, port_specified=False, domain='my_domain.lol', domain_specified=False, domain_initial_dot=False, path='/my_path', path_specified=False, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={}, rfc2109=False)]>

I tried several things with requests, first:

[74]: s.cookies.set('my_cookie','new_value')
Out[74]: Cookie(version=0, name='my_cookie', value='new_value', port=None, port_specified=False, domain='/mydomain.lol', domain_specified=False, domain_initial_dot=False, path='/my_path', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False)

but in return I got

In [75]: s.cookies
Out[75]: <RequestsCookieJar[Cookie(version=0, name='my_cookie', value='new_value', port=None, port_specified=False, domain='/mydomain.lol', domain_specified=False, domain_initial_dot=False, path='/my_path', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False), 
Cookie(version=0, name='my_cookie', value='new_value', port=None, port_specified=False, domain='mydomain.lol', domain_specified=False, domain_initial_dot=False, path='/my_path', path_specified=False, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={}, rfc2109=False)]>

As you can see, my new value did not replace the old one be created a new cookie, the same result was obtained using:

s.cookies['my_cookie'] = 'new_value'

I then tried specifying as many things as I could when setting my cookie and it worked :

In [67]: s.cookies.set('my_cookie','new_value',domain='mydomain.lol',path='/my_path')
Out[67]: Cookie(version=0, name='my_cookie', value='new_value', port=None, port_specified=False, domain='mydomain.lol', domain_specified=True, domain_initial_dot=False, path='/my_path', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False)

In [68]: s.cookies
Out[68]: <RequestsCookieJar[Cookie(version=0, name='my_cookie', value='new_value', port=None, port_specified=False, domain='mydomain.lol', domain_specified=True, domain_initial_dot=False, path='/my_path', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False)]>

Therefore my question, isn't there a more convinient way of setting a cookie without specifying so many things? By getting the first cookie of my array for example?

Upvotes: 4

Views: 5562

Answers (2)

Andreas
Andreas

Reputation: 9197

For me worked:

for cookie in s.cookies:
    if cookie.name == 'my_cookie':
        cookie.value = 'new_value'
        break

Upvotes: 0

Padraic Cunningham
Padraic Cunningham

Reputation: 180401

You can first set the value to None:

  s.cookies.set('cookie', None)
  s.cookies.set('cookie', "new_value")

An example:

In [5]: import requests


In [6]: with requests.Session() as s:
   ...:         s.get('http://httpbin.org/cookies/set?c1=foo&c2=bar')
   ...:         r = s.get('http://httpbin.org/cookies')
   ...:         print(r.text)
   ...:         s.cookies.set('c1', None)
   ...:         s.cookies.set('c1', "foobar")
   ...:         print(s.cookies)
   ...:         r = s.get('http://httpbin.org/cookies')
   ...:         print(r.text)
   ...:     
{
  "cookies": {
    "c1": "foo", 
    "c2": "bar"
  }
}

<<class 'requests.cookies.RequestsCookieJar'>[<Cookie c1=foobar for />, <Cookie c2=bar for httpbin.org/>]>
{
  "cookies": {
    "c1": "foobar", 
    "c2": "bar"
  }
}

Upvotes: 4

Related Questions