Take the 2-minute tour ×

I'm trying to use the Requests library to send cookies with a post request, but I'm not sure how to actually set up the cookies based on its documentation. The script is for use on Wikipedia, and the cookie(s) that need to be sent are of this form:

enwiki_session=17ab96bd8ffbe8ca58a78657a918558e; path=/; domain=.wikipedia.com; HttpOnly

However, the requests documentation quickstart gives this as the only example:

cookies = dict(cookies_are='working')

How can I encode a cookie like the above using this library? Do I need to make it with python's standard cookie library, then send it along with the POST request?

share|improve this question
Your cookie consists of a number of a=b; pairs. At a guess, use a as the key and b as the value in a dictionary. –  Thomas K Aug 23 '11 at 17:05

2 Answers 2

up vote 25 down vote accepted

The latest release of Requests will build CookieJars for you from simple dictionaries.

import requests

cookie = {'enwiki_session': '17ab96bd8ffbe8ca58a78657a918558'}

r = requests.post('http://wikipedia.org', cookies=cookie)

Enjoy :)

share|improve this answer
Working perfectly! Thank you! –  Ricardo Altamirano Aug 23 '11 at 17:30

Just to extend on the previous answer, if you are linking two requests together and want to send the cookies returned from the first one to the second one (for example, maintaining a session alive across requests) you can do:

import requests
r1 = requests.post('http://www.yourapp.com/login')
r2 = requests.post('http://www.yourapp.com/somepage',cookies=r1.cookies)
share|improve this answer
Additionally, you can use requests.session for this exact thing, storing cookies across multiple sessions, making calls from the returned session object instead. –  TankorSmash May 26 '12 at 0:35
I've had to utilize this even when using sessions at times. Sessions seem to miss Set-Cookie headers in some situations. –  kervin Apr 30 '13 at 3:08
@kervin this just happened to me as well. Seems like a bug in requests, because session should handle that. –  deweydb Aug 8 '13 at 11:05
@TankorSmash there is definitely a bug, my cookies are not carried forward using request.session –  Tjorriemorrie Sep 2 at 9:02
Is this the best way to set a (missed) cookie in a session? stackoverflow.com/a/17240616/143397 –  meowsqueak Dec 21 at 6:02

Your Answer


