urlEncode()

The function encodes a string into a percent-encoded string to be used within a URL.

Synopsis

string urlEncode(string decoded);

Parameters

Parameter Description
decoded A string to encode.

Return value

The function returns a percentage-encoded string.

Description

In some cases when using webservices (e.g. posting a Tweet on Twitter), you need to percent-encode a string. This is required when calling the netPost() function with "Content-Type", "application/x-www-form-urlencoded".

Therefore, you need the functions: urlDecode() and urlEncode(). The function urlEncode() encodes the decoded parameter into URL encoding format and urlDecode() decodes it back to a string.

For more information on URL encoding, see https://en.wikipedia.org/wiki/URL_encoding.

Encoding example

Note: This is a code excerpt!
 
// ...
string content = "code=" + urlEncode(query.code) +
"&grant_type=authorization_code" +
"&client_id=" + urlEncode(CLIENT_ID) +
"&redirect_uri=" + urlEncode(REDIRECT_URI) +
"&code_verifier=" + urlEncode(oauth2_code_verifier);
                
mapping result;
netPost("https://api.twitter.com/2/oauth2/token", 
makeMapping("headers", makeMapping("Authorization", "Basic " + base64Encode(CLIENT_ID + ":" + CLIENT_SECRET),
"Content-Type", "application/x-www-form-urlencoded"),"content", content), result);
// ...        

Availability

UI, CTRL