Back to Examples

URL encode/decode operations

Ballerina URL API supports encoding/decoding a URL or part of a URL.

For more information on the underlying module, see the url module.

import ballerina/io;
import ballerina/url;

public function main() returns error? {
    string value1 = "data=value";
    // Encoding a URL component into a string.
    string encoded = check url:encode(value1, "UTF-8");
    io:println("URL encoded value: ", encoded);

    string value2 = "data%3Dvalue";
    // Decoding an encoded URL component into a string.
    string decoded = check url:decode(value2, "UTF-8");
    io:println("URL decoded value: ", decoded);
}

To run this sample, place the source code in a file named url_encode_decode.bal and use the bal run command.

$ bal run url_encode_decode.balURL encoded value: data%3DvalueURL decoded value: data=value
PreviousJWT issue/validate
NextUTC time