# Request actions

Create request actions to dynamically assign variables when the response from the endpoint is ready. ARC gives you a convenient editor to create actions that are performed each time the request is sent.

Actions can be conditional. Create a condition rule for the request action and the action is performed only if all conditions are met.

This page describes in detail how to use request actions and conditions.

![Request actions overview](https://2300023309-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LgJa7u7Cw8HjSR0oUlx%2F-LrWUHgmUVpXRAdz6z7W%2F-LrWV5c8Lr-2tFxQLDTN%2Fimage.png?alt=media\&token=ec226b96-0116-4561-89b4-185cfd780fea)

There are two groups of actions that can be performed during the request

* request actions - executed before the connection is made
* response actions - executed when the response is ready

## Request actions

Currently request action only allows you to set a value on a variable before the request is made. This value is not stored in the data store but rather in memory only. This means when you restart the application the variable value will be restored to its original value.

![Request action with definition](https://2300023309-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LgJa7u7Cw8HjSR0oUlx%2F-LrWUHgmUVpXRAdz6z7W%2F-LrWW1wjt759YleXilOh%2Fimage.png?alt=media\&token=32661ec2-02f3-4d96-98f8-5b1a5ec80b82)

In this example, a `myAccessToken` variable is set to empty string before the request is executed.

## Response action

Response actions are more complex as they are made to extract data from the response and assign the value to a variable or store the variable in the data store.

The data can be extracted from each part of the request/response parts of the HTTP message: URL, headers, status line, and the payload.

### Extracting the data from the response

You need to tell the application where to look for the data that you want to extract from the response. In the editor, you can select either `Request` or `Response` as the main data source. In both cases, the rest of the configuration is the same but it uses either request or response values.

The next step is to define what type of data the application should query for the value. It can be `Url`, `Status code`, `Headers` or `Body`.&#x20;

Except for the status code, you should also define the path to the data. Depending on the selected type you can use different paths.

#### Path to the data

**URL**

If you do not specify the path the application will use the whole URL of the last response (it can be more than one response if there was redirection). You can specify the following paths for `url`:

* host - Returns the host value, e.g. `api.domain.com`
* `protocol` - Returns URL's protocol, e.g. `https:`
* `path` - URL's path, e.g. `/path/to/resource.json`
* `query` - Returns full query string, e.g. `version=1&page=test`
* `query.[any string]` - Returns the value of a query parameter. For `query.version` it would return `1`and for `query.page` the value will be `test`.
* `hash` - Returns everything that is after the `#` character, e.g. `access_token=token&state=A6RT7W`
* `hast.[any string]` - It treats hash as query parameters and returns the value of the parameter. For `hash.access_token` it would return `token`

**Example for URL type**

`https://auth.domain.com/auth/oauth-popup?version=2&remember=true#access_token=z8a1d97c-c4e6-488f-8ac0-a32e3d749f49&token_type=bearer&state=Y2I1CD`

```javascript
path = 'host' // auth.domain.com
path = 'protocol' // https:
path = 'path' // /auth/oauth-popup
path = 'query' // version=2&remember=true
path = 'query.version' // 2 (String!)
path = 'hash' // access_token=z8a1d97c-c4e6-488f-8ac0-a32e3d749f49&token_type=bearer&state=Y2I1CD
path = 'hash.access_token' // z8a1d97c-c4e6-488f-8ac0-a32e3d749f49
```

**Headers**&#x20;

Set header name as a path and the value of the header will be extracted from request or response.

**Example for headers**

```http
Content-Type: application/json
Content-Length: 100
Connection: close
```

```javascript
path = 'content-type' // application/json
path = 'Content-length' // 100 (String!)
path = 'Connection' // close
```

**Body**

Currently, only **JSON** and **XML** responses are supported. Also, XML has to be a valid XML string or the parser will not produce the value.

For JSON types simply specify the path to the data. To access array value use dot with index notation, for example `data.0.name`. This will get value `name` from the first item of the `data` array.

**Example for JSON**

```javascript
{
  property: {
    otherProperty: {
      value: 123456
    }
  }
}
path = 'property.otherProperty.value' // 123456
```

**Example for JSON array**

```javascript
{
  "data": [{
    "name": "a"
  }, {
    "name": "b"
  }]
}
path = 'data.1.name' // b
```

**XML**

Similar for XML:

```javascript
const xml = `<?xml version="1.0"?>
<people xmlns:xul="some.xul">
  <person db-id="test1">
    <name first="george" last="bush" />
    <address street="1600 pennsylvania avenue" city="washington" country="usa"/>
    <phoneNumber>202-456-1111</phoneNumber>
  </person>
</people>`
path = 'people.person.0.phoneNumber' // 202-456-1111
```

**Accessing XML attribute value**

XML path supports attr(ATTRIBUTE NAME) function that returns the value of the attribute:

```
path = 'people.person.0.name.attr(first)' // george
```

### Defining the resulting action

When `path` is set you can define two actions to be performed on the data. You can either permanently `Store variable` in the application's internal database or `Assign variable` temporarily until you restart the application. The last item is to define a variable name that will be updated with the value.

![Response action definition](https://2300023309-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LgJa7u7Cw8HjSR0oUlx%2F-LrWUHgmUVpXRAdz6z7W%2F-LrWb5obtbLoylmmFGA7%2Fimage.png?alt=media\&token=e265b484-4297-476e-8332-2e9d63d4a3c5)

## Conditions

![](https://2300023309-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LgJa7u7Cw8HjSR0oUlx%2F-LrWUHgmUVpXRAdz6z7W%2F-LrWb_xX4A0rWJp6FnNl%2Fimage.png?alt=media\&token=945f3b39-3bd6-4428-857d-9f33b1c7b688)

You can add a condition to the action so the action will be executed if all defined conditions are met.

To add a condition to the action click on the `Add condition` button. Source, Type, and Path to data work the same way as in Action editor.

After you define the source of the data choose the operator to be used to compare the data. It can be one of:

* equal
* not-equal
* greater-than
* greater-than-equal
* less-than
* less-than-equal
* contains

Contains can operate on strings, whole headers object (contains "content-type") and on JSON objects (contains "property").

The last field to set up is `Condition value` which is used to compare the data.

## Further reading

See our guide to [authorize the application with OAuth 2 and request actions](https://docs.advancedrestclient.com/guides/authenticating-with-oauth2-and-request-actions).
