psf / psf/requests

How to propagate cookie policies through posts?

Open
#5,449 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
54.3k
Forks
10.4k
Avg merge
16h 43m
Merged PRs (30d)
3

Description

Hi,

I am using Python 3.8 and the latest pip updates of everything mentioned below.

I am trying to consume a very old, but active SOAP resource. I am using Zeep for the SOAP part. The SOAP service is so old that I need to set a special cookie policy to handle the fact that the old SOAP service returns obsolete set-cookie2 headers. I am new to this so there could be better ways but the first few executable lines of my code sample (at the bottom) create a dummy cookie to establish a cookie policy that should pull in the obsolete cookies.

THe dummy cookie is attached to the session and the SOAP "login" method is called. I have stepped through the execution path (most recent on top). Everything above the "transports" lines is in the your domain I think.

		merge_cookies, cookies.py:549
		prepare_request, sessions.py:441
		request, sessions.py:516
		post, sessions.py:578
		post, transports.py:61
		post_xml, transports.py:107
		send, soap.py:122
		__call__, proxy.py:40
		<module>, zeep_test.py:31

I have modified Zeep's code to send the session cookies to your "post" method in the "kwargs".

        response = self.session.post(
            address, data=message, headers=headers,
            timeout=self.operation_timeout, 
			cookies=self.session.cookies
        )

"post" calls "request" calls "prepare_request" calls "merge_cookies". That code is:

        # Merge with session cookies
        merged_cookies = merge_cookies(
            merge_cookies(RequestsCookieJar(), self.cookies), cookies)

In my case the passed in cookie is a "cookielib.CookieJar". The line:

        try:
            cookiejar.update(cookies)

is executed but the "cookielib.CookieJar" policies are NOT copied over. I don't know what to do about that. Is there a better way to set a global cookie policy?

A relevant portion of my code is:

		import requests
		from requests import Session, cookies
		from zeep import Client, Settings
		from zeep.cache import SqliteCache
		from zeep.transports import Transport
		from http.cookiejar import CookieJar, DefaultCookiePolicy

		policy = DefaultCookiePolicy(rfc2965=True)
		cj = CookieJar(policy)
		cj.set_cookie(requests.cookies.create_cookie(name="dummy", value="dummy"))

		my_session = Session()
		my_session.cookies = cj

		my_settings = Settings(strict=False)
		my_cache = SqliteCache()
		my_url = 'https://lists.mycompany.com/lists/wsdl'

		client = Client(
			my_url,
			transport=Transport(
				session=my_session,
				cache=my_cache
			),
			settings=my_settings
		)
		
		response = client.service.login(email="Me",
										password="mememe")

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the CookieJar case described in requests/cookies.py:549 and sessions.py:441, where merge_cookies is called during prepare_request. Trace the request path through sessions.py and compare the resulting cookie behavior with the policy configured on http.cookiejar.CookieJar. Done means the supported behavior or required change is established and covered by an appropriate regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.