import ballerina/http;

service /sample on new http:Listener(9090) {

    // The path param is defined as a part of the resource path along with the type and it is extracted from the
    // request URI.
    resource function get path/[string foo](http:Request req) returns json {

        // Gets the [MatrixParams](https://docs.central.ballerina.io/ballerina/http/latest/classes/Request#getMatrixParams).
        map<any> pathMParams = req.getMatrixParams("/sample/path");
        var a = <string>pathMParams["a"];
        var b = <string>pathMParams["b"];
        string pathMatrixStr = string `a=${a}, b=${b}`;

        map<any> fooMParams = req.getMatrixParams("/sample/path/" + foo);
        var x = <string>fooMParams["x"];
        var y = <string>fooMParams["y"];
        string fooMatrixStr = string `x=${x}, y=${y}`;
        json matrixJson = {"path": pathMatrixStr, "foo": fooMatrixStr};

        // Create a JSON payload with the extracted values.
        json responseJson = {
            "pathParam": foo,
            "matrix": matrixJson
        };
        // Send a response with the JSON payload to the client.
        return responseJson;
    }
}

Matrix Parameter

Ballerina supports extracting MatrixParam values using http:Request support method.

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

import ballerina/http;
service /sample on new http:Listener(9090) {
    resource function get path/[string foo](http:Request req) returns json {

The path param is defined as a part of the resource path along with the type and it is extracted from the request URI.

        map<any> pathMParams = req.getMatrixParams("/sample/path");
        var a = <string>pathMParams["a"];
        var b = <string>pathMParams["b"];
        string pathMatrixStr = string `a=${a}, b=${b}`;

Gets the MatrixParams.

        map<any> fooMParams = req.getMatrixParams("/sample/path/" + foo);
        var x = <string>fooMParams["x"];
        var y = <string>fooMParams["y"];
        string fooMatrixStr = string `x=${x}, y=${y}`;
        json matrixJson = {"path": pathMatrixStr, "foo": fooMatrixStr};
        json responseJson = {
            "pathParam": foo,
            "matrix": matrixJson
        };

Create a JSON payload with the extracted values.

        return responseJson;
    }
}

Send a response with the JSON payload to the client.

bal run http_matrix_param.bal
[ballerina/http] started HTTP/WS listener 0.0.0.0:9090
# Run the cURL command below to invoke the service.
curl "http://localhost:9090/sample/path;a=4;b=5/value1;x=10;y=15"
{"pathParam":"value1", "matrix":{"path":"a=4, b=5", "foo":"x=10, y=15"}}