Swan Lake Beta2 is here.
import ballerina/url;
import ballerina/io;

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);
}

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/url;
import ballerina/io;
public function main() returns error? {
    string value1 = "data=value";
    string encoded = check url:encode(value1, "UTF-8");
    io:println("URL encoded value: ", encoded);

Encoding a URL component into a string.

    string value2 = "data%3Dvalue";
    string decoded = check url:decode(value2, "UTF-8");
    io:println("URL decoded value: ", decoded);
}

Decoding an encoded URL component into a string.

bal run url_encode_decode.bal
URL encoded value: data%3Dvalue
URL decoded value: data=value
import ballerina/url;
import ballerina/io;

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);
}

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/url;
import ballerina/io;
public function main() returns error? {
    string value1 = "data=value";
    string encoded = check url:encode(value1, "UTF-8");
    io:println("URL encoded value: ", encoded);

Encoding a URL component into a string.

    string value2 = "data%3Dvalue";
    string decoded = check url:decode(value2, "UTF-8");
    io:println("URL decoded value: ", decoded);
}

Decoding an encoded URL component into a string.

bal run url_encode_decode.bal
URL encoded value: data%3Dvalue
URL decoded value: data=value

In the creation of Ballerina, we were inspired by so many technologies. Thank you to all that have come before us (and forgive us if we missed one): Java, Go, C, C++, D, Rust, Haskell, Kotlin, Dart, TypeScript, JavaScript, Python, Perl, Flow, Swift, Elm, RelaxNG, NPM, Crates, Maven, Gradle, Kubernetes, Docker, Envoy, Markdown, GitHub and WSO2.

Cookie Policy

This website uses cookies so that we can provide you with the best user experience. Read our Cookie Policy to find out more.

If you wish to disable cookies you can do so from your browser.

I Understand