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.

  1. # Create a new session
  2. $ http --session=/tmp/session.json example.org API-Token:123
  3.  
  4. # Re-use an existing session — API-Token will be set:
  5. $ 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:

  1. $ 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:

  1. $ http --session=user1 example.org

To create or reuse a different session, simple specify a different name:

  1. $ http --session=user2 -a user2:password example.org X-Bar:Foo

Named sessions’s data is stored in JSON files in the the sessionssubdirectory 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:

  1. $ http --session=/tmp/session.json example.org
  2. $ http --session=/tmp/session.json admin.example.org
  3. $ http --session=~/.httpie/sessions/another.example.org/test.json example.org
  4. $ 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.