encodeURIComponent

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.

encodeURIComponent(String): String

Escapes certain characters in a URI component using UTF-8 encoding.

There can be only four escape sequences for characters composed of two "surrogate" * characters. encodeURIComponent escapes all characters except the following: alphabetic, decimal digits, - _ . ! ~ * ' ( ). Note that encodeURIComponent differs from encodeURI in that it encodes reserved characters and the Number sign # of encodeURI:

Type Includes

Reserved characters

Unescaped characters

alphabetic, decimal digits, - _ . ! ~ * ' ( )

Number sign

Parameters

Name Description

text

URI component string.

Example

This example encodes a variety of URI components.

Source

%dw 2.0
import * from dw::core::URL
output application/json
---
{
  "comparing_encode_functions_output" : {
  	"encodeURIComponent" : encodeURI(" PATH/ TO /ENCODE "),
  	"encodeURI" : encodeURI(" PATH/ TO /ENCODE "),
  	"encodeURIComponent_to_hex" : encodeURIComponent(";,/?:@&="),
  	"encodeURI_not_to_hex" : encodeURI(";,/?:@&="),
  	"encodeURIComponent_not_encoded" : encodeURIComponent("-_.!~*'()"),
  	"encodeURI_not_encoded" : encodeURI("-_.!~*'()")
  }
}

Output

{
  "comparing_encode_functions_output": {
    "encodeURIComponent": "%20PATH/%20TO%20/ENCODE%20",
    "encodeURI": "%20PATH/%20TO%20/ENCODE%20",
    "encodeURIComponent_to_hex": "%3B%2C%2F%3F%3A%40%26%3D",
    "encodeURI_not_to_hex": ";,/?:@&=",
    "encodeURIComponent_not_encoded": "-_.!~*'()",
    "encodeURI_not_encoded": "-_.!~*'()"
  }
}

Was this article helpful?

💙 Thanks for your feedback!

Edit on GitHub