3

I'm doing this right now, but it fails at that last line with TypeError: expected string or buffer.

import requests
from urllib.parse import urlparse

url = 'some url'

s = requests.Session()
s.headers.update({
    'Origin':urlparse(url).netloc,
    'Referer':url
})


r = s.get(url)

s.cookies['cookie1'] = 25
s.cookies['cookie2'] = 25

r = s.post(
    url,
    {'param':'value1', 'param2':'value2'},
    headers={'X-Requested-With':'XMLHttpRequest'}
)

What's the correct way to update the cookies when using Session? I'm pretty new to Python, so I might have confused something. Using Python 3.4.1.

Traceback:

Traceback (most recent call last):
  File "file.py", line 37, in <module>
    {'param':'value1', 'param2':'value2'}
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/sessions.py", line 498, in post
    return self.request('POST', url, data=data, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/sessions.py", line 422, in request
    prep = self.prepare_request(req)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/sessions.py", line 360, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/models.py", line 296, in prepare
    self.prepare_cookies(cookies)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/models.py", line 491, in prepare_cookies
    cookie_header = get_cookie_header(self._cookies, self)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/cookies.py", line 134, in get_cookie_header
    jar.add_cookie_header(r)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/cookiejar.py", line 1329, in add_cookie_header
    attrs = self._cookie_attrs(cookies)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/cookiejar.py", line 1288, in _cookie_attrs
    self.non_word_re.search(cookie.value) and version > 0):
TypeError: expected string or buffer
Markus Hedlund
  • 23,374
  • 22
  • 80
  • 109
  • `cookies['cookie1'] = '25'` etc... (I guess your example could be `r.cookies.update(cookie1='25', cookie2='25')` - as you're effectively updating the cookies on the request object itself (unless you mean `cookies = r.cookies.copy()`. It will help if you included the full traceback though... – Jon Clements Aug 26 '14 at 07:49
  • Changed the code slightly and added a traceback in the question. – Markus Hedlund Aug 26 '14 at 08:24

1 Answers1

9

Cookie values are strings, not integers. Set them as such:

s.cookies['cookie1'] = '25'
s.cookies['cookie2'] = '25'

Demo:

>>> import requests
>>> from urllib.parse import urlparse
>>> url = 'http://httpbin.org/cookies'
>>> s = requests.Session()
>>> s.headers.update({
...     'Origin':urlparse(url).netloc,
...     'Referer':url
... })
>>> r = s.get(url)
>>> s.cookies['cookie1'] = '25'
>>> s.cookies['cookie2'] = '25'
>>> r = s.get(url, headers={'X-Requested-With':'XMLHttpRequest'})
>>> print(r.text)
{"cookies": {"cookie1": "25", "cookie2": "25"}}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 4
    It's important to say that when you add a cookie through this dict-like interface (i.e `mySession.cookies["cookie_name"] = "cookie_value"`), it will be used on **all** domains that you send requests to. If you need a cookie to only be used on a specific domain, use the [.set() method](https://requests.readthedocs.io/en/master/user/quickstart/#cookies): `mySession.cookies.set("cookie_name", "cookie_value", domain=".specific-domain.com")` If you add this in the code above, the `cookie_name` cookie will not be sent because its base domain won't match with the requested domain ("httpbin.org"). – R. Navega Jun 05 '20 at 02:10