Skip to main content

Variables

Variables#

Variables can be defined in an HTTP file using the syntax: var name = "some value". Once defined, they can be reused throughout the file.

Supported Data Types#

  1. Math Expressions:
    Syntax: var var2 = (2 + 3 + var1);

    • Parentheses are mandatory.
  2. String Interpolations:
    Syntax: var var3 = $'ranga {var2}';

    • The $ symbol is mandatory.
  3. JSON Data:
    Syntax: var var4 = {'ram': 'ranga'};

  4. Numbers:
    Syntax: var var4 = 10;

  5. Boolean Values:
    Syntax:

    • var var5 = true;
    • var var5 = false;
  6. Functions:

    • var var6 = randomInt(2);
      Generates a two-digit integer.
    • var var1 = randomStr(2);
      Generates a two-letter string.
    • var var2 = randomFloat(3);
      Generates a random float.
    • var var3 = uuid();
      Generates a UUID.
    • var var4 = randomSlug();
      Generates a slug.
    • var var5 = timestamp();
      Generates the current timestamp.

Once defined, these variables can be used anywhere using the syntax {{var_name}}.

Example:#

var var_name = "ranga";
var query_key = "query_key";
var query = "query_value";
POST "https://httpbin.org/POST
?"{{query_key}}"="{{query_value}}"
json({
"var_name": {{var_name}}
})

Dothttp Environment & Properties#

In scenarios where defining variables directly in HTTP files is not ideal (e.g., containing sensitive information like secrets), an environment file can be used to define variables. The environment name can be passed during execution, and all properties in the environment file will be automatically applied.

Environment File Example: .dothttp.json#

{
"*": {
"default": "variables"
},
"john": {
"name": "jhonny",
"lastname": "depp"
},
"ironman": {
"name": "robert",
"lastname": "dny jnr"
}
}

HTTP Example:#

// Place in the same directory as the HTTP file.
// Enable the environment by passing `--env ironman`.
POST "https://httpbin.org/post"
json({
"ironman": "{{name}}",
"lastName": "{{lastname}}"
})

Properties#

Properties are useful for setting individual variables without defining an entire environment.

System Command Variables#

Variables and properties in an environment file or passed through the command line are static. For time-sensitive variables (e.g., access_token), use System Command Variables to generate them dynamically.

Example:#

{
"$commands": {
"access_token": "az account get-access-token"
}
}

In this example, the access_token variable is generated by running the specified command when accessed.

OS Environment Variables#

To use OS environment variables in an HTTP file, prefix the variable name with DOTHTTP_ENV_.

Example:#

Define or export a variable, such as DOTHTTP_ENV_date, and access it as {{date}} in the HTTP request:

GET https://req.dothttp.dev/get
json({
"data": "{{date}}"
})

Math Expressions#

Math expressions can be used for calculations that should not be immediately evaluated. Dothttp handles their expansion and substitution.

Example:#

POST https://httpbin.org/post
json(
{
# Instead of using 3600 directly, use 1*60*60.
"secondsInHour": "{{$expr:1*60*60}}"
}
)