Swan Lake Beta3 is here.
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"]}
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"]}
Subscribe to the newsletter

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