3

I need to send cookies in a specific, not random order.

Python requests use a dictionary to manage cookies, so the predefined cookies I set are always in a random order.

Also, the subsequent cookies from the session are placed randomly after the predefined cookies.

import requests

s = requests.Session()

predefined_cookies = dict(cookie1='a', cookie2='b', cookie3='c')
r = s.get('http://eu.httpbin.org/cookies/set?k1=v1&k2=v2', cookies=predefined_cookies)
r = s.get('http://eu.httpbin.org/cookies/set?k3=v3&k4=v4', cookies=predefined_cookies)

Outcome:

cookie2=b; cookie1=a; cookie3=c; k2=v2; k1=v1
cookie2=b; cookie1=a; cookie3=c; k3=v3; k2=v2; k4=v4; k1=v1

Ideally, I'd like to send the predefined cookies in an order they're written, but more importantly, I need to place a session cookie I want at a very end, so for example place a k4=v4 cookie at the very end.

This doesn't have to be a session. I'd appreciate your input.

Community
  • 1
  • 1
Bart
  • 524
  • 7
  • 17
  • I'm not sure it's a good idea to depend on the order of cookies. RFC2109 says: _"If multiple cookies satisfy the criteria above, they are ordered in the Cookie header such that those with more specific Path attributes precede those with less specific. Ordering with respect to other attributes (e.g., Domain) is unspecified."_ That is, reordering by Request library may make sense (wrt specificity), and depending on order otherwise on the server side may be perilous, because other user-agents will not stick to it. – 9000 May 23 '15 at 03:44
  • 1
    I know it's not ideal, but I need it for the situation. – Bart May 23 '15 at 04:01

0 Answers0