Executing example ad-hoc commands
Prev | Chapter 2. REST API | Next

## Executing example ad-hoc commands

### Retrieving list of active users

#### Using XML

To execute the command to get a list of active users, make a request using POST method for `/rest/adhoc/sess-man@domain.com` sending the following content (request requires authentication using Basic HTTP Authentication):

    <command>
      <node>http://jabber.org/protocol/admin#get-active-users</node>
      <fields>
        <item>
          <var>domainjid</var>
          <value>domain.com</value>
        </item>
        <item>
          <var>max_items</var>
          <value>25</value>
        </item>
      </fields>
    </command>

In this request we passed all the parameters needed to execute adhoc command. We passed the node of the adhoc command and values for fields required by that command. We passed values of "domain.com" for "domainjid" field and "25" for "max_items" field. We also need to pass `Content-Type: text/xml` to HTTP header of request or add `type` parameter set to `text/xml`.

### Note

In case of multi value fields use following format:

    <value>
        <item>first-value</item>
        <item>second-value</item>
    </value>

Below is example result for request presented above:

    <command>
      <jid>sess-man@domain.com</jid>
      <node>http://jabber.org/protocol/admin#get-active-users</node>
      <fields>
        <item>
          <var>Users: 2</var>
          <label>text-multi</label>
          <value>admin@domain.com</value>
          <value>user1@domain.com</value>
        </item>
      </fields>
    </command>

#### Using JSON

To execute the command to get active users in JSON format, make a request using POST method for /rest/adhoc/sess-man@domain.com sending the following content (this request also requires authentication using Basic HTTP Authentication):

    {
      "command" : {
        "node" : "http://jabber.org/protocol/admin#get-active-users",
        "fields" : [
          {
            "var" : "domainjid",
            "value" : "domain.com"
          },
          {
            "var" : "max_items",
            "value" : "25"
          }
        ]
      }
    }

In this request we passed all parameters needed to execute adhoc command. We passed the node of adhoc command and values for fields required by adhoc command. In this case we passed value of "domain.com" for "domainjid" field and "25" for "max_items" field.

Below is an example result for request presented above:

    {
        "command": {
            "jid": "sess-man@domain.com",
            "node": "http://jabber.org/protocol/admin#get-active-users",
            "fields": [
                {
                    "var": "Users: 1",
                    "label": "text-multi",
                    "value": [
                      "admin@domain.com",
                      "user1@domain.com"
                    ]
                }
            ]
        }
    }

Prev | Up | Next
Retrieving list of available adhoc commands | Home | Ending a user session