How to send cookies in a post request with the Python Requests library?

329,875

Solution 1

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

import requests

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

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

Enjoy :)

Solution 2

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:
329,875

Related videos on Youtube

Ricardo Altamirano
Author by

Ricardo Altamirano

Originally from Nicaragua, educated in Edinburgh and the USA, and now living primarily in London. Useful questions and answers Stack Overflow Debugging CREATE TABLE statements Logging in Python Simple string formatting in Python Reference types in C# String compression in C# using Gzip Meta Stack Overflow Book recommendation questions Answering old questions with a solution in the comments IT Security Cryptographically secure random strings in PHP LaTeX Fitting a table on a page through rotation StackExchange Flair

Updated on July 28, 2020

Comments

  • Ricardo Altamirano
    Ricardo Altamirano almost 4 years

    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?

    • Thomas K
      Thomas K over 12 years
      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.
  • TankorSmash
    TankorSmash almost 12 years
    Additionally, you can use requests.session for this exact thing, storing cookies across multiple sessions, making calls from the returned session object instead.
  • Viktor Vix Jančík
    Viktor Vix Jančík about 11 years
    I've had to utilize this even when using sessions at times. Sessions seem to miss Set-Cookie headers in some situations.
  • deweydb
    deweydb over 10 years
    @kervin this just happened to me as well. Seems like a bug in requests, because session should handle that.
  • Tjorriemorrie
    Tjorriemorrie over 9 years
    @TankorSmash there is definitely a bug, my cookies are not carried forward using request.session
  • davidA
    davidA over 9 years
    Is this the best way to set a (missed) cookie in a session? stackoverflow.com/a/17240616/143397
  • alphazwest
    alphazwest about 7 years
    Way old answer, but helped me a lot. For some reason, I'm having issues with cookie persistence when using a Session() object, but this worked beautifully.
  • Chris Nielsen
    Chris Nielsen over 6 years
    Is this code supposed to place a cookie in my browser? I tried it and it didn't work for me.
  • ThiefMaster
    ThiefMaster over 6 years
    @ChrisNielsen this question/answer has nothing to do with browsers
  • Chris Nielsen
    Chris Nielsen over 6 years
    @ThiefMaster: Normally, cookies live in browsers. If this answer doesn't have to do with browsers, what does it have to do with?
  • jouell
    jouell almost 6 years
    Same issue and same solution.. just FYI on requests v.2.19.1
  • DDay
    DDay over 4 years
    @ChrisNielsen: This question and the code in the answer is about setting a cookie in a Python request. The request does something similar to a browser request, but no browsers are involved.
  • crifan
    crifan over 3 years
    Additionally for @TankorSmash, how to use requests.session? can refer: stackoverflow.com/a/31571805/1616263
  • serv-inc
    serv-inc over 2 years
    @ChrisNielsen : cookies are an aspect of the basic http protocol, which is most often used by browsers: datatracker.ietf.org/doc/html/rfc6265