cookiejar.set_cookie(create_cookie(name, cookie_dict[name]))
return cookiejar
+
+
+def merge_cookies(cookiejar, cookies):
+ """Add cookies to cookiejar and returns a merged CookieJar.
+
+ :param cookiejar: CookieJar object to add the cookies to.
+ :param cookies: Dictionary or CookieJar object to be added.
+ """
+ if not isinstance(cookiejar, cookielib.CookieJar):
+ raise ValueError('You can only merge into CookieJar')
+
+ if isinstance(cookies, dict):
+ cookiejar.update(
+ cookiejar_from_dict(
+ cookies, cookiejar=cookiejar, overwrite=False))
+ elif isinstance(cookies, cookielib.CookieJar):
+ cookiejar.update(cookies)
+
+ return cookiejar
from collections import Mapping
from datetime import datetime
-from .compat import cookielib, OrderedDict, urljoin, urlparse, builtin_str
-from .cookies import cookiejar_from_dict, extract_cookies_to_jar, RequestsCookieJar
+from .compat import cookielib, OrderedDict, urljoin, urlparse, urlunparse, builtin_str
+from .cookies import (
+ cookiejar_from_dict, extract_cookies_to_jar, RequestsCookieJar, merge_cookies)
from .models import Request, PreparedRequest
from .hooks import default_hooks, dispatch_hook
from .utils import to_key_val_list, default_headers
prep = self.prepare_request(req)
# Add param cookies to session cookies
- if isinstance(cookies, dict):
- self.cookies = cookiejar_from_dict(
- cookies, cookiejar=self.cookies, overwrite=False)
- elif isinstance(cookies, cookielib.CookieJar):
- self.cookies.update(cookies)
+ self.cookies = merge_cookies(self.cookies, cookies)
proxies = proxies or {}