Free MuleSoft CONNECT Keynote & Expo Pass Available!

Register now+
Nav

encodeURIComponent

encodeURIComponent(String): String

The encodeURIComponent() function encodes a Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character (will only be four escape sequences for characters composed of two "surrogate" characters).

encodeURIComponent escapes all characters except the following: alphabetic, decimal digits, - _ . ! ~ * ' ( ) 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

Transform
[source,DataWeave, linenums]
%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("-_.!~*'()")
  },
  "comparing_decode_function_output": {
  	"decodeURIComponent" : decodeURIComponent("%20PATH/%20TO%20/DECODE%20"),
  	"decodeURI" : decodeURI("%20PATH/%20TO%20/DECODE%20"),
  	"decodeURIComponent_from_hex" : decodeURIComponent("%3B%2C%2F%3F%3A%40%26%3D"),
  	"decodeURI_from_hex" : decodeURI("%3B%2C%2F%3F%3A%40%26%3D"),
  	"decodeURIComponent_from_hex" : decodeURIComponent("%2D%5F%2E%21%7E%2A%27%28%29%24"),
  	"decodeURI_from_hex" : decodeURI("%2D%5F%2E%21%7E%2A%27%28%29%24")
  }
}
Output

        
     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
  "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": "-_.!~*'()"
  },
  "comparing_decode_function_output": {
    "decodeURIComponent": " PATH/ TO /DECODE ",
    "decodeURI": " PATH/ TO /DECODE ",
    "decodeURIComponent_from_hex": ";,/?:@&=",
    "decodeURI_from_hex": ";,/?:@&=",
    "decodeURIComponent_from_hex": "-_.!~*'()$",
    "decodeURI_from_hex": "-_.!~*'()$"
  }
}