Переменные окружения NativeRest

Variables enable you to store and reuse values in NativeRest. By storing a value as a variable, you can reference it throughout a request body, headers, tests, and query params.

A variable is a symbolic representation of data that enables you to access a value without having to enter it manually wherever you need it. This can be useful if you are using the same values in multiple places. Variables make your requests more flexible and readable, by abstracting the detail away.

For example, if you have the same URL in more than one request, but the URL might change later, you can store the URL in a variable host and reference it in your requests using {{host}}. If the URL changes, you can change the variable value and it will be reflected throughout your requests, wherever you've used the variable name.

The same principle applies to any part of your request where data is repeated. Whatever value is stored in the variable will be included wherever you've referenced the variable when your requests run. If the base URL value is https://nativerest.net/echo, and is listed as part of the request URL using {{host}}/get, NativeRest will send the request to https://nativerest.net/echo/get.

Click Variables on the main menu or use shortcut Ctrl + E to open the environment editor.

NativeRest variables panel

Variables in NativeRest are key-value pairs.
You can use symbol # at the beginning of a line to enter a comment.
Each variable name represents its key, so referencing the variable name enables you to access its value.

# My first name
myName = Antonio

# My token
myToken = ABCD0123

Empty lines, comments, additional spaces are allowed. To format a list of variables using a shortcut Ctrl + Alt + L.

Variable scopes

NativeRest supports variables at different scopes, allowing you to tailor your processing to a variety of development, testing, and collaboration tasks. Scopes in NativeRest relate to the different contexts that your requests run in, and different variable scopes are suited to different tasks.

In order from broadest to narrowest, these scopes are: system, global, workspace, and environment.

  • System variables enable you to generate sample data, including random names, addresses, email addresses, and much more. You can use these pre-defined variables multiple times to return different values per request.
  • Global variables enable you to access data between collections, requests, scripts, and environments. Global variables are available throughout a workspace. Since global variables have the broadest scope available in NativeRest, they're well-suited for testing and prototyping. In later development phases, use more specific scopes.
  • Workspace variables are available throughout the requests in a workspace and are independent of environments. Workspace variables don't change based on the selected environment. Workspace variables are suitable if you're using a single environment, for example for auth or URL details.
  • Environment variables enable you to scope your work to different environments, for example local development versus testing or production. One environment can be active at a time.

NativeRest variable scopes

If a variable with the same name is declared in two different scopes, the value stored in the variable with narrowest scope will be used. For example, if there is a global variable named username and an environment variable named username, the local value will be used when the request runs.

Set variables from a response

Starting from NativeRest 2.0.3 it has become possible to set variables from the response. Before setting a value to a variable, be sure to declare it. You can declare a variable in any scope: global, workspace and environment.

NativeRest set variable from a response

You can use JSON Path to extract the value from the response body. It is also possible to get the value from headers and cookies. Various examples:

{{country}} = response.body.users[1].location.country
{{firstName}} = response.body.users[1].name.first
{{myToken}} = response.body.token
{{serverHeader}} = response.headers["Server"]
{{dateHeader}} = response.headers["Date"]
{{responseTime}} = response.time
{{responseSize}} = response.size
{{statusCode}} = response.status

Using variables

You can use double curly braces to reference variables throughout NativeRest. For example, to reference a variable named username in your request authorization settings, you would use the following syntax with double curly braces around the name:

{{username}}

When you run a request, NativeRest will resolve the variable and replace it with its current value. For example, you could have a request URL referencing a variable as follows:

https://nativerest.net/echo/get?customerId={{custId}}

NativeRest will send whatever value you currently have stored for the custId variable when the request runs. If custId is currently 3, the request will be sent to the following URL including the query parameter:

https://nativerest.net/echo/get?customer_id=3

If you are want to access a variable from within a request body, wrap its reference in double-quotes:

{ "customerId": "{{custId}}" }

You can use variables in request URLs, parameters, headers, authorization, body, and header presets.

When you hover over a variable, NativeRest shows an overview of its current status. As you add variables to your requests, NativeRest prompts you with any that are currently defined.