Skip to content

Commit

Permalink
Add documentation for cookie priority in sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
kbanc committed Jun 18, 2020
1 parent 5b98e4a commit 06af281
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
56 changes: 55 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,6 @@ Simple example:
$ http PUT httpbin.org/put name=John [email protected]
.. code-block:: http
PUT / HTTP/1.1
Accept: application/json, */*;q=0.5
Accept-Encoding: gzip, deflate
Expand Down Expand Up @@ -1739,6 +1738,61 @@ exchange after it has been created, specify the session name via
# But it is not updated:
$ http --session-read-only=./ro-session.json httpbin.org/headers Custom-Header:new-value
Cookie Storage Behaviour
------------------------
**TL;DR:** Cookie storage priority: Server response > Command line request > Session file
To set a cookie within a Session there are three options:
1. Get a `Set-Cookie` header in a response from a server
.. code-block:: bash
$ http --session=./session.json httpbin.org/cookie/set?foo=bar
2. Set the cookie name and value through the command line as seen in `cookies`_
.. code-block:: bash
$ http --session=./session.json httpbin.org/headers Cookie:foo=bar
3. Manually set cookie parameters in the json file of the session
.. code-block:: json
:emphasize-lines: 12-19
{
"__meta__": {
"about": "HTTPie session file",
"help": "https://httpie.org/doc#sessions",
"httpie": "2.2.0-dev"
},
"auth": {
"password": null,
"type": null,
"username": null
},
"cookies": {
"foo": {
"expires": null,
"path": "/",
"secure": false,
"value": "bar"
}
}
}
Cookies will be set in the session file with the priority specified above. For example, a cookie
set through the command line will overwrite a cookie of the same name stored
in the session file. If the server returns a `Set-Cookie` header with a
cookie of the same name, the returned cookie will overwrite the preexisting cookie.
Expired cookies are never stored. If a cookie in a session file expires, it will be removed before
sending a new request. If the server expires an existing cookie, it will also be removed from the
session file.
Config
======
Expand Down
2 changes: 1 addition & 1 deletion httpie/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def update_headers(self, request_headers: RequestHeadersDict):
if name.lower() == 'cookie':
for cookie_name, morsel in SimpleCookie(value).items():
self['cookies'][cookie_name] = {'value': morsel.value}
del request_headers['Cookie']
del request_headers[name]
continue

for prefix in SESSION_IGNORED_HEADER_PREFIXES:
Expand Down

0 comments on commit 06af281

Please sign in to comment.