Swan Lake Beta2 is here.
import ballerina/http;

// The `absolute resource path` represents the absolute path to the service. When bound to a listener
// endpoint, the service will be accessible at the specified path. If the path is omitted, then it defaults to `/`.
// A string literal also can represent the absolute path. E.g., `"/foo"`.
// The `type descriptor` represents the respective type of the service. E.g., `http:Service`.
service http:Service /foo on new http:Listener(9090) {

    // The `resource method name` (`post`) confines the resource to the specified HTTP methods. In this
    // instance, only `POST` requests are allowed. The `default` accessor can be used to match with all methods
    // including standard HTTP methods and custom methods.
    // The `resource path` associates the relative path to the service object's path. E.g., `bar`.
    resource function post bar(@http:Payload json payload) returns json {
        return payload;
    }
}

Service Path and Resource Path

Ballerina supports writing RESTful services according to the JAX-RS specification. You can use the absolute-resource-path and resource-name to access a resource function while the accessor-name, which is an HTTP verb as post and get to constrain your resource function in a RESTful manner.

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

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

The absolute resource path represents the absolute path to the service. When bound to a listener endpoint, the service will be accessible at the specified path. If the path is omitted, then it defaults to /. A string literal also can represent the absolute path. E.g., "/foo". The type descriptor represents the respective type of the service. E.g., http:Service.

    resource function post bar(@http:Payload json payload) returns json {
        return payload;
    }
}

The resource method name (post) confines the resource to the specified HTTP methods. In this instance, only POST requests are allowed. The default accessor can be used to match with all methods including standard HTTP methods and custom methods. The resource path associates the relative path to the service object’s path. E.g., bar.

bal run absolute_path_and_path.bal
[ballerina/http] started HTTP/WS listener 0.0.0.0:9090
Run the cURL command below to invoke the resource.
curl http://localhost:9090/foo/bar -d "{\"hello\": \"world\"}" -H "Content-Type: application/json"
{"hello":"world"}
import ballerina/http;

// The `absolute resource path` represents the absolute path to the service. When bound to a listener
// endpoint, the service will be accessible at the specified path. If the path is omitted, then it defaults to `/`.
// A string literal also can represent the absolute path. E.g., `"/foo"`.
// The `type descriptor` represents the respective type of the service. E.g., `http:Service`.
service http:Service /foo on new http:Listener(9090) {

    // The `resource method name` (`post`) confines the resource to the specified HTTP methods. In this
    // instance, only `POST` requests are allowed. The `default` accessor can be used to match with all methods
    // including standard HTTP methods and custom methods.
    // The `resource path` associates the relative path to the service object's path. E.g., `bar`.
    resource function post bar(@http:Payload json payload) returns json {
        return payload;
    }
}

Service Path and Resource Path

Ballerina supports writing RESTful services according to the JAX-RS specification. You can use the absolute-resource-path and resource-name to access a resource function while the accessor-name, which is an HTTP verb as post and get to constrain your resource function in a RESTful manner.

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

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

The absolute resource path represents the absolute path to the service. When bound to a listener endpoint, the service will be accessible at the specified path. If the path is omitted, then it defaults to /. A string literal also can represent the absolute path. E.g., "/foo". The type descriptor represents the respective type of the service. E.g., http:Service.

    resource function post bar(@http:Payload json payload) returns json {
        return payload;
    }
}

The resource method name (post) confines the resource to the specified HTTP methods. In this instance, only POST requests are allowed. The default accessor can be used to match with all methods including standard HTTP methods and custom methods. The resource path associates the relative path to the service object’s path. E.g., bar.

bal run absolute_path_and_path.bal
[ballerina/http] started HTTP/WS listener 0.0.0.0:9090
Run the cURL command below to invoke the resource.
curl http://localhost:9090/foo/bar -d "{\"hello\": \"world\"}" -H "Content-Type: application/json"
{"hello":"world"}

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