Skip to main content
Version: 4.15.0

Script component

The Script component allows you to modify messages using scripts written in Groovy or JavaScript. The scripts can either be provided directly or uploaded as files. Scripts are able to read and modify the body, headers, and properties of messages that pass through it.

To simplify the development of scripts, the Script component offers the ability to evaluate scripts given a set of test values for the body, headers, and properties of a message.

Migration Guide

If you migrate from version 4.14.x or older then check the Scripts migration guide for new version of Groovy and JavaScript.

Configuration

The Script component has the following configuration options:

Upload method

Select if you want to enter the script manually in or through uploading a file.

Options:

  • Enter script manually (default)
  • Upload file

Script

The script that should be evaluated for each message passing through the component.

note

Only available when Upload method is set to Enter script manually.

Upload File

The script that should be evaluated for each message uploaded in the form of a file.

note

Only available when Upload method is set to Upload file.

Language

The language used for the script.

Options:

  • JavaScript (default)
  • Groovy
Tip: Prefer Groovy over JavaScript whenever possible

Groovy, like Camel, runs in the JVM (Java Virtual Machine) while Javascript runs via Node.js (and therefore has a higher impact on performance).

note

The JavaScript that you can use in the Script component is not the same implementation as in web browsers. It is an open source implementation of JavaScript for Java. We advice that you use standard JavaScript syntax also known as ES5. Newer syntax and other features are not supported at the moment.

In Headers

A list of comma-separated name-value pairs representing headers for testing purposes. The names and values should be quoted.

define in headers

Use the following format: "headerName"="value","anotherHeaderName"="value",etc

In Properties

A list of comma-separated name-value pairs representing properties for testing purposes. The names and values should be quoted.

in properties format

Use the following format: "propertyName"="value","anotherPropertyName"="value",etc

In Body

A body for testing purposes.

Out Headers

Shows the list of headers after validating the script. Not user editable.

Out Properties

Shows the list of properties after validating the script. Not user editable.

Out Body

Shows the body of the message after validating the script. Not user editable.

Validate script

Use this button to validate your script without having to install the flow. The In Headers, In Properties and In Body are used for the validation. The Out Headers, Out Properties and Out Body will be shown in their respective fields.

Examples

Below there are examples of what can be done in the Script component. Also see the Using time in Dovetail guide for more Groovy script examples.

Working with headers

JavaScript
// Get header 'myHeaderName' as is
var headerAsIs = message.getHeader("myHeaderName");
// Get header 'myHeaderName' as String
var headerAsString = message.getHeader("myHeaderName").toString();
// Get header 'myHeaderName' as Integer
var headerAsInt = parseInt(message.getHeader("myHeaderName").toString(), 10);

// Overwrite existing header 'myHeaderName'
message.setHeader("myHeaderName", "header value");

// Set new header 'newHeaderName'
message.setHeader("newHeaderName", "new header value");

// Remove header 'removeHeaderName'
message.removeHeader("removeHeaderName");

// Put data on the body
body = headerAsIs+headerAsString+headerAsInt;
Groovy
// Get header 'myHeaderName' as is
def headerAsIs = request.getHeader("myHeaderName");
// Get header 'myHeaderName' as String
def headerAsString = request.getHeader("myHeaderName", String.class);
// Get header 'myHeaderName' as Integer
def headerAsInt = request.getHeader("myHeaderName", Integer.class);

// Overwrite existing header 'myHeaderName'
request.setHeader("myHeaderName", "header value");

// Set new header 'newHeaderName'
request.setHeader("newHeaderName", "new header value");

// Remove header 'removeHeaderName'
request.removeHeader("removeHeaderName");

// Put data on the body
body = headerAsIs+headerAsString+headerAsInt;

Working with the body

JavaScript
// Get body as is
var bodyAsIs = body;
// Get body as String
var bodyAsString = body.toString();
// Get body as Integer
var bodyAsInt = parseInt(body, 10);

// Set the body
bodyAsIs = "My new body";

// The last used variable is returned as the body by default
// In this example: "My new body"
JavaScript - If you don't work with the body
// Set this at the end of the script to prevent the body becoming null
body;
Groovy
// Get the body from the message
// Get the body as is
def bodyAsIs = request.body;
// Get the body as String
def bodyAsString = request.getBody(String.class);
// Get the body as Integer
def bodyAsInt = request.getBody(Integer.class);

// Set the body
bodyAsIs = "My new body";

// The last used variable is returned as the body by default
// In this example: "My new body"
Groovy - If you don't work with the body
// Set this at the end of the script to prevent the body becoming null
result = request.body;

Import third party libraries in JavaScript scripts

Use the import function to import third party libraries to be used in JavaScript scripts:

import("https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.min.js");
import("https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/hmac-sha256.min.js");
import("https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/enc-base64.min.js");

Full example with script evaluation

The image below shows the result of evaluating a script for a given body, set of headers and set of properties.

Script component example
note

In the Flow Designer you enter test data in text inputs. If those text inputs are empty, the script is actually tested with empty strings. An empty text input is therefore not considered null. If the flow is running and the body is empty, it will be null and not "". If you convert this empty body to string you will get "null". It is best to test your script with null for the In Body.

Versions

DovetailGroovy versionJavascript version (Engine)
< 4.14.x2.5ECMAScript 5.1 (Nashorn)
> 4.15.03.0 Release notesECMAScript 2023 (GraalVM)
> 4.17.04.0 Release notesECMAScript 2023 (GraalVM)
Last update on Apr 22, 2024