Skip to content

Unable to override cookie policy in Session.prepare_request #3416

@bdusell

Description

@bdusell

I would like to be able to override the cookie policy used by a Session when making a request or when using a Session to prepare a request. However, overriding the cookie policy on the cookie jar of the Session or of the Request object has had no effect. This line of code explains why:

https://github.com/kennethreitz/requests/blob/master/requests/sessions.py#L377

The cookie jars of both the session and the request are merged into a newly constructed RequestsCookieJar, discarding the policies of both. I believe there should be a way to inject a non-default cookie policy at this point.

What I expected to be able to do, and what isn't working currently, is something like the following:

import requests
s = requests.Session()
s.cookies = requests.cookies.RequestsCookieJar(policy=MyCustomCookiePolicy())
s.get('http://httpbin.org/cookies/set?k2=v2&k1=v1') # Put some cookies in the jar
r = requests.Request('GET', 'http://httpbin.org/cookies')
pr = s.prepare_request(r) # Include certain cookies in the jar based on arbitrary rules
# Print the request data for debugging
s.send(pr)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions