import ballerina/http;

// The `absolute resource path` can be omitted. Then, it defaults to `/`.
service on new http:Listener(9090) {

    // The `default` accessor name can be used to match with all methods including standard HTTP methods
    // and custom methods. The rest param is used to represent the wildcard of the `resource path` in which any path
    // segment will get dispatched to the resource in the absence of an exact path match.
    resource function 'default [string... paths](http:Request req)
            returns json {
        return {method: req.method, path: paths};
    }
}

Default Resource

Ballerina provides rest params in the resource path and the default resource method to help designing proxy services and default resources. It can be used to handle unmatched requests.

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

import ballerina/http;
service on new http:Listener(9090) {

The absolute resource path can be omitted. Then, it defaults to /.

    resource function 'default [string... paths](http:Request req)
            returns json {
        return {method: req.method, path: paths};
    }
}

The default accessor name can be used to match with all methods including standard HTTP methods and custom methods. The rest param is used to represent the wildcard of the resource path in which any path segment will get dispatched to the resource in the absence of an exact path match.

bal run http_default_resource.bal
[ballerina/http] started HTTP/WS listener 0.0.0.0:9090
Run the cURL command below with the unmatched path and method.
curl "http://localhost:9090/foo/bar" -X POST
{"method":"POST", "path":["foo", "bar"]}