Variables
Variables can be used to store intermediate results during flow execution. A valid variable name starts with $ followed by a letter a…z or A…Z or an underscore _. More letters, underscores, the numbers 0…9 or hyphens - may follow.
Predefined Variables
The following predefined variables exist:
$body: client request body$env: environment variables$request: client request information$server: server information$upstream: upstream response information$error: Contains information regarding the most recent error, but is initially empty.
Try the following flow with
$ curl --data hello localhost:8080 | jq<flow>
<template>
{
"$request": {
{{with $request }}{{: * }}{{end}}
},
"$body": {{ $body }},
"$upstream": {{ $upstream }},
"$server": {
{{: $server/* }}
},
"$env": {
{{: $env/* }}
}
}
</template>
</flow>The actions request and requests set the $upstream variable, too:
Defining and Accessing Variables
Global variables are usually defined as the output of eval actions. The variable name is defined in the out="…" attribute. It must begin with $ followed by a letter and then more letters or numbers.
For structured JSON variables, you can use the template action:
Variables may be copied with eval, too:
Local Variables
Templates may define local variables with {{$… := …}}. Those variables are undefined outside the template they're defined in:
📎 If a variable containing binary content is processed in a
templateorxsltaction, its content will probably end up being truncated, garbled or both.
Undefined Variables
Attempting to access a variable that has not been set previously will yield an empty node-set. The empty node-set will be evaluated to false in conditions and produces the string null in placeholders:
$body
$bodyThe $body variable contains the request body.
If the request body is JSON (Content-Type: application/json) $body contains the parsed JSON. You can access its properties with XPath expressions with a json segment before the top-level properties. E.g.
The value for foo can be accessed by $body/json/foo, the value for baz by $body/json/bar/baz.
In other cases the content is stored in $body as a string and cannot be accessed by XPath.
$request
$requestThe $request variable contains information about the incoming client request, such as the URL, the request header fields and possibly the query component or cookies, if any were sent.
Example:
As HTTP request headers are defined to be case-insensitive, their names are lower-cased for convenient access even if the client has sent the field name with upper-case letters, e.g.:
If a client URL path is matched by a wildcard path, $request/endpoint is the path part preceding the part matched by /**. Otherwise, $request/endpoint is the same as $request/path.
Client URL
matches
$request/path
$request/endpoint
$upstream
$upstreamThe $upstream variable contains information about upstream responses. The properties for each upstream response are stored with the request ID (id property or content attribute).
url- The request URL (string)status- The response status code (integer)cacheHit-trueif the response was served from a cache (seeuse-http-cacheorforce-cache-ttlrequest options),falseotherwiseheaders- The response headers, each with a lower-cased field name
Example:
To check, for example, if the status code of the response with the ID myRequest is successful you can use the following XPath expression:
$error
$errorClient request and response validation, upstream connection and request and response validation errors, and those triggered by the error action will store information about the error in $error. While initially empty, $error will have the following properties containing information about the most recent error, unless it is triggered by the error action:
status- the HTTP status that is used by default for responses if the error was passed to the client (type:number)code- an error code (type:number)message- a single line of text describing the error (type:string)info- detailed information about the error (type:arrayofstring)requestID- the requestID as it should appear in the logs (type:string)
Example:
If set by the error action, $error contains the JSON template result of the action's element body.
See also
Using Env Vars (cookbook)
Last updated
Was this helpful?