not_a_pointer
not_a_pointer

Reputation: 3

Sending cookies in Python

I have some cookies in python stored like this:

    cookie = [
{"""
    "domain": ".justdial.com",
    "expirationDate": 1577653041.993055,
    "hostOnly": false,
    "httpOnly": true,
    "name": "_ctk",
    "path": "/",
    "sameSite": "no_restriction",
    "secure": false,
    "session": false,
    "storeId": "0",
    "value": "893b0b69e25c0359d6e1fd88f16fea90a4bd2e0e8f8356e80bfc572e7f7e1343",
    "id": 1"""
},
{"""
    "domain": ".justdial.com",
    "expirationDate": 1546136368,
    "hostOnly": false,
    "httpOnly": false,
    "name": "_fbp",
    "path": "/",
    "sameSite": "no_restriction",
    "secure": false,
    "session": false,
    "storeId": "0",
    "value": "fb.1.1546114608524.1389346931",
    "id": 2"""
}
]

requests.post(URL, cookies=cookie)

I am trying to send these cookies using Requests, but that does not work. Is the format wrong, or the way that I am sending it?

Thanks for the help! Using RequestsCookieJar worked, but I found another way: I saved it to a separate file, then, using the json library I got it in the right format and was able to send the cookies.

Upvotes: 0

Views: 616

Answers (1)

JacobIRR
JacobIRR

Reputation: 8946

In your code, cookie is a list. You need to send a dict, or you can use the requests.cookies.RequestsCookieJar() object:

From the docs:

>>> jar = requests.cookies.RequestsCookieJar()
>>> jar.set('tasty_cookie', 'yum', domain='httpbin.org', path='/cookies')
>>> jar.set('gross_cookie', 'blech', domain='httpbin.org', path='/elsewhere')
>>> url = 'https://httpbin.org/cookies'
>>> r = requests.get(url, cookies=jar)
>>> r.text
'{"cookies": {"tasty_cookie": "yum"}}'

Upvotes: 1

Related Questions