4.2.2.15.3. HTTP Client Expressions: Constants, Metadata and Scripting
Throughout the HTTP Client's configuration settings you will find that you regularly have to input "expressions". Expressions are different types of input that will eventually be interpreted as text. Babelway supports three types of expressions:
- Constant: The text you provide will be used exactly as it is typed.
- Metadata: This is plain text as well except all words surrounded with curly braces will be replaced with the matching metadata value.
- Script: A piece of code that, once interpreted, provides a text value.
Constant
Let's say we are in the "request" tab of one of your HTTP calls and we are inputing the URL for your call. The below example will call exactly this URL:
https://dev.babelway.net/rest/testmyhttp/message.edi
Metadata
The metadata field is similar to the constant field except it attempts a replacement of all text between curly braces with the matching metadata. Thus, if in my context I have a set a metadata called "product" that contains the value "apples". The following expression will evaluate to this URL:
https://mylocation.com/rest/pushProducts/apples
Script
The last and most elaborate expression type is "script". It allows you to compute values or configuration settings. Our gateway expects you to use JavaScript in these expressions.
For security reasons we use a black box to run the Javascript code and it does not contain many libraries. The Script engine is fully compatible with ECMAScript 5.1. However, this is a very old version of JavaScript which we aim to upgrade. In the meantime, if you run into functions that do not work, reach out to our support for alternatives.
We have enhanced this scripting context with a set of useful functions and predefined variables.
Predefined variables
By default Babelway will create the below system variables for each call and their values can be parsed and manipulated when selecting the expression of type "Script". For example, one can tweak the "responseContent" in a "Post Action" > "Create message" script.
- loginResponseContent : The body of the login response, saved as text.
- loginResponseContentBytes :The body of the login response saved as a byte array.
- loginResponseHeaders : The headers of the login response saved as key-value pairs. To get the value of the login response header use:
loginResponseHeaders.get("header_name")
- loginResponseCode : The status code of the login response.
- responseContent : The body of the response saved as text.
- responseContentBytes : The body of the response save as a byte array.
- responseHeaders : The headers of the response saved as key-value pairs. To get the value of the response header use::
responseHeaders.get("header_name")
- responseCode : The status code of the response.
- responseCookies: The cookies of the response saved as key-value pairs. To get the value of a cookie use:
responseCookies.get('sessionId');
Note:
- The expression of type "Script" is case sensitive. You will need to make sure to use the above variables with the propre case. So for example, responseContent will contain the body of your response, responsecontent will be empty.
- For each call the old system variables will be overridden with the new response information. So if you want to use an old system variable in other calls then you must save its value in another variable (for example in "Post Action" > "Extract variable").
Functions
HttpClientFunctions.xmlToJson(String data)
Decodes a Base64 encoded string.
data | The XML string to convert. |
returns | The converted JSON string. |
The below example shows the use of the xmlToKson(data) function from the "HttpClientFunctions" class which will generate the JSON object matching your XML. This is particularly useful when you wish to navigate an XML response using script.
HttpClientFunctions.xmlToJson("<person><name>Carlo Catarinicchia</name><age>40</age><address><city>Antwerp</city></address></person>");
The above script will provide you with the following JSON object:
{
"person": {
"name": "Carlo Catarinicchia",
"age": 40,
"address": {
"city": "Antwerp"
}
}
}
So, for example, if the responseContent from your call were to be the XML from this example, then you could use its content in script like below:
HttpClientFunctions.xmlToJson(responseContent).person.name
HttpClientFunctions.HmacSHA256(String data, String key, Boolean, base64Encode)
Computes an HMAC using SHA-256 hashing algorithm.
data | The input string to hash. |
key | The key used for HMAC computation. |
base64Encode | If true, the output is Base64 encoded; otherwise, it is hexadecimal. |
returns | The computed HMAC string. |
The below example shows the use of the HmacSHA256() function from the "HttpClientFunctions" class which will generate the base64 value of the HmacSHA256 of "message" with the key "secret", as shown below.
HttpClientFunctions.HmacSHA256('message','secret',true);
HttpClientFunctions.HmacMD5(String data, String key, Boolean base64Encode)
Computes an HMAC using MD5 hashing algorithm.
data | The input string to hash. |
key | The key used for HMAC computation. |
base64Encode | If true, the output is Base64 encoded; otherwise, it is hexadecimal. |
returns | The computed HMAC string. |
HttpClientFunctions.HmacSHA1(String data, String key, Boolean base64Encode)
Computes an HMAC using SHA-1 hashing algorithm.
data | The input string to hash. |
key | The key used for HMAC computation. |
base64Encode | If true, the output is Base64 encoded; otherwise, it is hexadecimal. |
returns | The computed HMAC string. |
HttpClientFunctions.HmacSHA512(String data, String key, Boolean, base64Encode)
Computes an HMAC using SHA-256 hashing algorithm.
data | The input string to hash. |
key | The key used for HMAC computation. |
base64Encode | If true, the output is Base64 encoded; otherwise, it is hexadecimal. |
returns | The computed HMAC string. |
HttpClientFunctions.HmacSHA512(String toEncode)
Encodes a string to Base64 format.
toEncode | The string to be encoded. |
returns | The Base64 encoded string. |
HttpClientFunctions.HmacSHA512(String toDecode)
Decodes a Base64 encoded string.
toDecode | The string to be decoded. |
returns | The Base64 decoded string. |
HttpClientFunctions.rsaEncrypt(String toEncrypt, String publicKeyAlias)
Encrypts a given string using RSA and a public key available in your environment.
toEncrypt | The string to encrypt. |
publicKeyAlias | The alias of the public key stored in your environment. (An error will be thrown if no certificate could be found matching that alias) |
returns | The Base64 decoded string. |