Transform your data

Restlet Client allows you to manipulate data via functions. Functions can create new data or transform existing data. The functions that transform data apply on the value of the expression they finish.

Usage example

I want to retrieve the 10 first planet names in the list of my planets in my Star wars API. I can use the expression: ${"Star-wars API"."Get 10 planet names"."response"."body".jsonPath("$.results[:10].name")}.

The method jsonPath parses the result of the expression ${"Star-wars API"."Get 10 planet names"."response"."body"}.

Constants

When writing an expression like ${"toto"}, the expression will be evaluated as:

  • The value of the current environment's variable toto if there is one. See how environment references work for more details.
  • If no environment variable matched, the request/scenario/service/project toto that is at root-level in your repository. See how repository references work for more details.
  • The string toto in any other case.

Table of content

Data creation

Random

Returns a random number in range 0 <= number < max.

Arguments:

  • max: defaults to 1000000000

Example:

  
Expression: ${random(50)}
Possible result: 42
  

Timestamp

Returns the number of milliseconds from January 1, 1970, 00:00:00 GMT to the date when it's evaluated.

Arguments: none

Example:

  
Expression: ${timestamp()}
Possible result: 1505136142950
  

UUID

Returns a UUID.

Arguments: none

Example:

  
Expression: ${uuid()}
Possible result: f5fafd92-2298-4e72-97c9-df4dabaf27d2
  

Data transformation

Base 64

Encodes in base 64.

Arguments: none

Example:

  
Expression: ${"username:password".base64()}
Result: dXNlcm5hbWU6cGFzc3dvcmQ=
  

HMAC

Creates a Hash-based Message Authentication Code .

Arguments:

  • cipher: the hash function
    • Options: SHA1, SHA224, SHA256, SHA384 or SHA512
  • key: the secret key
  • output: the output type
    • Options: Base64 or Hex

Example:

  
Expression:  ${"test".hmac("sha256", "secret", "Base64")}
Result:      Aymga2LNFrM+tnkr6MYLFY2Jou46h2/Omogeu0iMCRQ=

Expression:  ${"test".hmac("sha1", "secret", "Hex")}
Result:      1aa349585ed7ecbd3b9c486a30067e395ca4b356
  

JSON path

Returns the element(s) extracted from the JSON input with the given JSON path selector. See the JSON path documentation.

Arguments:

  • selector: The JSON path selector that points to the part of the JSON to extract from the input.

Example:

  
Expression: ${"{\"titi\": \"toto\"}".jsonPath("$.titi")}
Result: toto
  

Xpath

Returns the element(s) extracted from the XML input with the given XPath selector. See the XPath documentation.

Arguments:

  • selector: The XPath selector that points to the part of the XML to extract from the input.

Example:

  
Expression: ${"<title lang="en" />".xPath("//title/@lang")}
Result: [lang="en"]
  

Length

Computes the length of its input:

  • the number of characters if the input is a string.
  • the number of items in a JSON array
  • the number of keys in an JSON object

Arguments: none

Example:

  
Expression: ${"Star-wars API"."Get 2 planets"."response"."body"}
Result: {
  "count":61,
  "results": [
    {"name":"Alderaan" },
    {"name":"Naboo" },
    {"name":"Hoth" }
  ]
}

Expression: ${"Star-wars API"."Get 2 planets"."response"."body".length()}
Result: 2

Expression: ${"Star-wars API"."Get 2 planets"."response"."body"."results".length()}
Result: 3

Expression: ${"Star-wars API"."Get 2 planets"."response"."body"."results"."0"."name".length()}
Result: 8
  

Lower

Converts all the characters to lower case using the rules of the default locale.

Arguments: none

Example:

  
Expression: ${"AbC".lower()}
Result: abc
  

MD5

Calculates the MD5 hash of its input.

Arguments: none

Example:

  
Expression: ${"toto".md5()}
Result: f71dbe52628a3f83a77ab494817525c6
  

Sha

Hashes its input.

Arguments:

  • cipher: the hash function
    • Options: SHA1, SHA224, SHA256, SHA384 or SHA512
  • output: the output type
    • Options: Base64 or Hex

Example:

  
Expression: ${"toto".sha("SHA224", "Base64")}
Result: IcBD7s1+hUI6ctrjwGKitb+g5rNc4/54jJtXpg==

Expression: ${"toto".sha("SHA1", "Hex")}
Result: 0b9c2625dc21ef05f6ad4ddf47c5f203837aa32c
  

String

Quotes its input.

Arguments:

  • quote character: the character that should be used to quote the input.
    • Options: ' and "
    • Default: "

Example:

  
Expression: ${"toto".string()}
Result: "toto"

Expression: ${"toto".string("\"")}
Result: "toto"

Expression: ${"toto".string("'")}
Result: 'toto'
  

Substring

Returns a truncated input.

Arguments:

  • start: position of the first character that should be extracted
    • Default: 0
  • end: position of the first character that should be omitted
    • Default: end of the input

Note: the index of the first character is 0

Example:

  
Expression:  ${"0123456".substring(1)}
Result:      123456

Expression:  ${"0123456".substring(1, 2)}
Result:      1

Expression:  ${"0123456".substring(0, 4)}
Result:      0123
  

Upper

Converts all the characters to upper case using the rules of the default locale.

Arguments: none

Example:

  
Expression: ${"aBc".upper()}
Result: ABC
  

Going further

Expressions can do more, if you haven't read it yet, have a look at this page to learn more.