compose

DataWeave 2.2 is compatible and bundled with Mule 4.2. Standard Support for Mule 4.2 ended on May 2, 2021, and this version of Mule will reach its End of Life on May 2, 2023, when Extended Support ends.

Deployments of new applications to CloudHub that use this version of Mule are no longer allowed. Only in-place updates to applications are permitted.

MuleSoft recommends that you upgrade to the latest version of Mule 4 that is in Standard Support so that your applications run with the latest fixes and security enhancements.

compose(Array<String>, Array<String>): String

Uses a custom string interpolator to replace URL components with a encodeURIComponent result. You can call this function using the standard call, or a simplified version.

Parameters

Name Description

baseStringArray

A string array that contains the URL parts to interpolate using the strings in the interpolationStringArray parameter.

interpolationStringArray

A string array that contains the strings used to interpolate the baseStringArray.

Example

The following example uses the compose function to form an encoded URL, the first parameter is an array of two strings that are part of the URL and the second parameter is the urlPath variable that is used to interpolate the strings in the first parameter. Notice that the spaces in the input are encoded in the output URL as %20.

Source

%dw 2.0
output application/json
var urlPath = "content folder"
import * from dw::core::URL
---
{ "encodedURL" : compose(["http://examplewebsite.com/", "/page.html"], ["$(urlPath)"]) }

Output

{ "encodedURL" : "http://examplewebsite.com/content%20folder/page.html" }

Simplified Syntax

You can also call this function using the simplified syntax, which uses backticks (`) to enclose the string that includes the variable to encode.

Example

This example shows how to use the simplified syntax to obtain the same result as in the previous example.

Source

%dw 2.0
output application/json
var urlPath = "content folder"
import * from dw::core::URL
---
{ "encodedURL" : compose `http://examplewebsite.com/$(urlPath)/page.html` }

Output

{ "encodedURL" : "http://examplewebsite.com/content%20folder/page.html" }

Was this article helpful?

💙 Thanks for your feedback!

Edit on GitHub