Unable to override cookie policy
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.5k
- Forks
- 76
- Avg merge
- 8h 59m
- Merged PRs (30d)
- 24
Description
Originally opened by
@KKomarovon 2023-11-15 01:09:25 in encode/httpx
I would like to be able to override the cookie policy used by a Client when making a request. However, overriding the cookie policy on the cookie jar of the Client or of the Request object has had no effect. These lines of code explains why:
https://github.com/encode/httpx/blob/497b315fc7a0ca6a54c8ec6c929568abaf6efab1/httpx/_models.py#L334
https://github.com/encode/httpx/blob/497b315fc7a0ca6a54c8ec6c929568abaf6efab1/httpx/_models.py#L1030
When initialise request from existing cookies (which is Cookies object of Client), it discards policy object from source Cookies object.
https://github.com/encode/httpx/blob/497b315fc7a0ca6a54c8ec6c929568abaf6efab1/httpx/_client.py#L400
The cookie jars of both the client and the request are merged into a newly constructed CookieJar, discarding the policies of both.
What I expected to be able to do, and what isn't working currently, is something like the following:
cookie = MozillaCookieJar(filename='cookies.txt', policy=MyCookiePolicy())
cookie.load()
session = httpx.Client(headers=headers, cookies=cookie)
session.get(url)
In my opinion Policy from CookieJar in Client should not be discarded. When CookieJar object passed in request then policy from request should override policy from Client.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked sections of httpx/_models.py around lines 334 and 1030, then inspect httpx/_client.py around line 400. Trace how CookieJar objects are copied and merged, including the MozillaCookieJar example. Done means a policy supplied by the client is preserved, while a request-level policy overrides it.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend, networking
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 52/100