Sessions
By default, every request HTTPie makes is completely independent of anyprevious ones to the same host.
However, HTTPie also supports persistentsessions via the —session=SESSION_NAME_OR_PATH
option. In a session,custom HTTP headers (except for the ones starting with Content-
or If-
),authentication, and cookies(manually specified or sent by the server) persist between requeststo the same host.
- # Create a new session
- $ http --session=/tmp/session.json example.org API-Token:123
- # Re-use an existing session — API-Token will be set:
- $ http --session=/tmp/session.json example.org
All session data, including credentials, cookie data,and custom headers are stored in plain text.That means session files can also be created and edited manually in a texteditor—they are regular JSON. It also means that they can be read by anyonewho has access to the session file.
Named sessions
You can create one or more named session per host. For example, this is howyou can create a new session named user1
for example.org
:
- $ http --session=user1 -a user1:password example.org X-Foo:Bar
From now on, you can refer to the session by its name. When you choose touse the session again, any previously specified authentication or HTTP headerswill automatically be set:
- $ http --session=user1 example.org
To create or reuse a different session, simple specify a different name:
- $ http --session=user2 -a user2:password example.org X-Bar:Foo
Named sessions’s data is stored in JSON files in the the sessions
subdirectory of the config directory:~/.httpie/sessions/<host>/<name>.json
(%APPDATA%\httpie\sessions\<host>\<name>.json
on Windows).
Anonymous sessions
Instead of a name, you can also directly specify a path to a session file. Thisallows for sessions to be re-used across multiple hosts:
- $ http --session=/tmp/session.json example.org
- $ http --session=/tmp/session.json admin.example.org
- $ http --session=~/.httpie/sessions/another.example.org/test.json example.org
- $ http --session-read-only=/tmp/session.json example.org
Readonly session
To use an existing session file without updating it from the request/responseexchange once it is created, specify the session name via—session-read-only=SESSION_NAME_OR_PATH
instead.