Variable substitution

By craig | March 16, 2020

Today, we’re going to describe how to use variable substitution in ServeUp.

Variable substitution

ServeUp supports variable substitution in the response data for all fields (eg. HTTP body, HTTP Headers, Cookies, File name) using the Mustache syntax. An example of this is {{cookie.JSESSIONID}}. This would replace everything between the {{ and }} inclusively with the value of the JSESSIONID cookie.

The available variables are described below. By way of example, consider the following inbound request:

https://john:secret@www.myhost.com/api/user/1234?aaa=bbb&ccc=ddd#xyz
    Accept-Encoding: application/json
    Cookie: JSESSIONID=abcd

    { "name" : "Samantha", "pets" : [ "Rex", "Fido ] }

that matches the following URL matcher:

/api/user/(.*)?aaa=(.*)&.*

URL variables

  • docroot - The document root path (ie. where to find your files)
  • url - The full URL
  • scheme - The scheme of the URL => https
  • user - The user of the URL => john
  • password - The password of the URL => secret
  • host - The hostname that the request came to => www.myhost.com
  • port - The port that the request came to => 443
  • path - The path => /api/user/1234
  • query - Everything after the question mark => aaa=bbb&ccc=ddd
  • fragment - Everything after the hash => xyz

Request variables

  • parameter.NAME.X - The value of the X’th query string parameter for NAME. eg. {{parameter.aaa.0}} => bbb
  • match.X - The regular expression group of the URL matcher. eg. {{match.0}} => 1234, {{match.1}} => bbb
  • cookie.NAME - The value of cookie NAME. eg {{cookie.JSESSIONID}} => abcd
  • header.NAME - The value of HTTP header NAME. eg {{header.Accept-Encoding}} => application/json
  • form.NAME.X - The value of the X’th HTTP URL encoded form variable NAME. Not applicable in the above example as is not using form variables.
  • json.NAME - The value of a JSON element using NAME to extract the value. eg. json.name => Samantha , json.pets[1] => Fido. See the below for details on the supported syntax.

JSON variables

Similarly, many requests contain a JSON payload in the HTTP body. ServeUp supports a KVO/Xpath-like parsing syntax to extract values from the inbound JSON. For example, consider the case where the HTTP body contains:

{
  "name": "John",
  "pets": [
    {
      "name": "Pluto"
    },
    {
      "name": "Sylvester"
    }
  ],
  "addresses": [
    {
      "type" : "home",
      "address": {
        "number": 123,
        "street": "Smith St"
      }
    },
    {
      "type" : "work",
      "address": {
        "number": 47,
        "street": "Jones Rd"
      }
    }
  ]
}

The following simple syntaxes are supported:

  • name => John
  • pets[0].name => Pluto
  • pets[1].name => Sylvester
  • addresses[0].type => home
  • addresses[0].address.number => 123
  • addresses[1].type => work
  • addresses[1].address.street => Jones Rd

Calculated variables

  • substring(variable, length) - Length can only be < 20, and must be entered as _X_. eg. {{ substring(json.name, _4_) }} => Sama. If the input is less than length, then the entire input will be returned.
  • randomTiny - Generates a random int between 1 and 9. eg {{randomTiny}} might return 6
  • randomSmall - Generates a random int between 1 and 99
  • randomMedium - Generates a random int between 1 and 999
  • randomLarge - Generates a random int between 1 and 999,999
  • randomCurrency - Generates a random float between 1.0 and 999.9