> ## Documentation Index
> Fetch the complete documentation index at: https://docs.requestly.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Legacy globals and requestly

> The older global variables and the requestly object that scripts can still use in Requestly API client, with the rq.* API to use instead.

Older scripts read the request and the response through bare global variables (`request`, `responseBody`, `environment`) and change variables through an object called `requestly`. These still work, so scripts written that way keep running. They are deprecated: for new scripts, use the `rq.*` API each one points to.

The first time a script uses one of the objects or functions below, the console shows one warning for it that names the `rq.*` replacement. A script that uses the same name again does not warn again.

## Global variables

Each global is set up before your script runs and does not follow later changes.

| Global | What it holds | Use instead |
| - | - | - |
| `request` | A copy of the request: `id`, `name`, `description`, `method` (uppercase), `url` (as written, with `{{variables}}` unresolved), `headers`, and `data` | [`rq.request`](https://docs.requestly.com/api-client/rq-api-reference/rq-request) |
| `environment` | An object with the environment variables as they were when the script started | [`rq.environment`](https://docs.requestly.com/api-client/rq-api-reference/rq-environment) |
| `globals` | An object with the global variables as they were when the script started | [`rq.globals`](https://docs.requestly.com/api-client/rq-api-reference/rq-globals) |
| `data` | The current row of the data file, with the types the file gave it | [`rq.iterationData`](https://docs.requestly.com/api-client/rq-api-reference/rq-iteration-data) |
| `iteration` | The number of the current iteration | `rq.info.iteration` |
| `tests` | An object where each key is the name of a test and each value is its result | [`rq.test()`](https://docs.requestly.com/api-client/rq-api-reference/rq-test) |
| `responseBody` | The response body as text. Not defined when the body is over 50 MB | `rq.response.text()` |
| `responseCode` | `{ name, detail, code, standardName }` for the response status | `rq.response.details()` |
| `responseHeaders` | The response headers, with each name as the server sent it. When a header repeats, the last value is kept | [`rq.response.headers`](https://docs.requestly.com/api-client/rq-api-reference/rq-response) |
| `responseCookies` | The cookies of the response, with `one(name)`, `get(name)` and `all()` | `rq.response.cookies` |
| `responseTime` | The response time in milliseconds | `rq.response.responseTime` |

The response globals exist only when the script has a response, so in a pre-request script they are not defined.

### Details

* `request.headers` has lowercase names. A header that appears more than once is an array, and a disabled header is left out.
* `request.data` is the raw text of a raw body. For a form body (urlencoded or form-data) it is an object of the parameters, and a parameter that repeats becomes an array. Without a body it is `{}`.
* Changing `request`, `environment` or `globals` changes only the copy in your script. The request that is sent and the variables you save do not change. To change them, use `rq.request`, `rq.environment` and `rq.globals`.
* `tests` is read when the main part of your script has finished. Each key becomes one test that passes when its value is truthy. A key you assign later, for example inside a callback, is not reported.

```javascript theme={null}
tests["Status is 200"] = responseCode.code === 200;
tests["Has an id"] = JSON.parse(responseBody).id !== undefined;
```

## The requestly object

`requestly` changes variables and the run order. Every member has a matching `rq.*` call.

| Member | What it does | Use instead |
| - | - | - |
| `requestly.setEnvironmentVariable(key, value)` | Sets an environment variable | `rq.environment.set()` |
| `requestly.getEnvironmentVariable(key)` | Reads an environment variable | `rq.environment.get()` |
| `requestly.clearEnvironmentVariable(key)` | Removes an environment variable | `rq.environment.unset()` |
| `requestly.clearEnvironmentVariables()` | Removes all environment variables | `rq.environment.clear()` |
| `requestly.setGlobalVariable(key, value)` | Sets a global variable | `rq.globals.set()` |
| `requestly.getGlobalVariable(key)` | Reads a global variable | `rq.globals.get()` |
| `requestly.clearGlobalVariable(key)` | Removes a global variable | `rq.globals.unset()` |
| `requestly.clearGlobalVariables()` | Removes all global variables | `rq.globals.clear()` |
| `requestly.setNextRequest(name)` | Sets the next request of a collection run | `rq.execution.setNextRequest()` |
| `requestly.getResponseHeader(name)` | Reads a response header. The name is not case sensitive | `rq.response.headers.get()` |
| `requestly.getResponseCookie(name)` | Reads a response cookie | `rq.cookies.get()` |

The two response members exist only when the script has a response.

The setters store a value that is truthy as its text, so `requestly.setEnvironmentVariable("count", 5)` stores `"5"`. A value that is falsy (`0`, `false`, an empty string) is stored as it is. The setters also update the `environment` and `globals` copies, so a script that mixes the two reads its own writes.

## Libraries

`tv4`, `Backbone` and `cheerio` are also available as globals. They load the first time you use them and warn once. Load them with `require('tv4')`, `require('backbone')` and `require('cheerio')` instead. See [Import packages into your scripts](https://docs.requestly.com/api-client/import-packages-into-your-scripts.md#supported-libraries).

## Where they are not defined

Whether code in a [custom package](https://docs.requestly.com/api-client/import-packages-into-your-scripts.md) can see these globals or `requestly` depends on the script mode, so do not rely on them there. Pass what the package needs as function arguments, or read it through `rq`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.