Swan Lake Beta2 is here.
import ballerina/http;

// HTTP version is set to 2.0.
http:Client http2serviceClientEP =
        check new ("http://localhost:7090", {httpVersion: "2.0"});

service / on new http:Listener(9090) {

    resource function 'default http11Service(http:Request clientRequest)
            returns json|error {
        // Forward the [clientRequest](https://docs.central.ballerina.io/ballerina/http/latest/classes/Request) to the `http2` service.
        json clientResponse = check
            http2serviceClientEP->forward("/http2service", clientRequest);

        // Send the response back to the caller.
        return clientResponse;

    }
}

// HTTP version is set to 2.0.
listener http:Listener http2serviceEP = new (7090,
    config = {httpVersion: "2.0"});

service / on http2serviceEP {

    resource function 'default http2service() returns json {
        // Send the response back to the caller (http11Service).
        return { 
            "response": {
                "message":"response from http2 service"
            }
        };
    }
}

HTTP 1.1 to 2.0 Protocol Switch

In this example, the Ballerina HTTP service receives a message over the HTTP/1.1 protocol and forwards it to another service over the HTTP/2.0 protocol.

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

import ballerina/http;
http:Client http2serviceClientEP =
        check new ("http://localhost:7090", {httpVersion: "2.0"});

HTTP version is set to 2.0.

service / on new http:Listener(9090) {
    resource function 'default http11Service(http:Request clientRequest)
            returns json|error {
        json clientResponse = check
            http2serviceClientEP->forward("/http2service", clientRequest);

Forward the clientRequest to the http2 service.

        return clientResponse;

Send the response back to the caller.

    }
}
listener http:Listener http2serviceEP = new (7090,
    config = {httpVersion: "2.0"});

HTTP version is set to 2.0.

service / on http2serviceEP {
    resource function 'default http2service() returns json {
        return { 
            "response": {
                "message":"response from http2 service"
            }
        };
    }
}

Send the response back to the caller (http11Service).

bal run http_1_1_to_2_0_protocol_switch.bal
[ballerina/http] started HTTP/WS listener 0.0.0.0:7090
[ballerina/http] started HTTP/WS listener 0.0.0.0:9090
curl http://localhost:9090/http11Service
{"response":{"message":"response from http2 service"}}

Invoke the HTTP/1.1 service using “cURL”.

import ballerina/http;

// HTTP version is set to 2.0.
http:Client http2serviceClientEP =
        check new ("http://localhost:7090", {httpVersion: "2.0"});

service / on new http:Listener(9090) {

    resource function 'default http11Service(http:Request clientRequest)
            returns json|error {
        // Forward the [clientRequest](https://docs.central.ballerina.io/ballerina/http/latest/classes/Request) to the `http2` service.
        json clientResponse = check
            http2serviceClientEP->forward("/http2service", clientRequest);

        // Send the response back to the caller.
        return clientResponse;

    }
}

// HTTP version is set to 2.0.
listener http:Listener http2serviceEP = new (7090,
    config = {httpVersion: "2.0"});

service / on http2serviceEP {

    resource function 'default http2service() returns json {
        // Send the response back to the caller (http11Service).
        return { 
            "response": {
                "message":"response from http2 service"
            }
        };
    }
}

HTTP 1.1 to 2.0 Protocol Switch

In this example, the Ballerina HTTP service receives a message over the HTTP/1.1 protocol and forwards it to another service over the HTTP/2.0 protocol.

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

import ballerina/http;
http:Client http2serviceClientEP =
        check new ("http://localhost:7090", {httpVersion: "2.0"});

HTTP version is set to 2.0.

service / on new http:Listener(9090) {
    resource function 'default http11Service(http:Request clientRequest)
            returns json|error {
        json clientResponse = check
            http2serviceClientEP->forward("/http2service", clientRequest);

Forward the clientRequest to the http2 service.

        return clientResponse;

Send the response back to the caller.

    }
}
listener http:Listener http2serviceEP = new (7090,
    config = {httpVersion: "2.0"});

HTTP version is set to 2.0.

service / on http2serviceEP {
    resource function 'default http2service() returns json {
        return { 
            "response": {
                "message":"response from http2 service"
            }
        };
    }
}

Send the response back to the caller (http11Service).

bal run http_1_1_to_2_0_protocol_switch.bal
[ballerina/http] started HTTP/WS listener 0.0.0.0:7090
[ballerina/http] started HTTP/WS listener 0.0.0.0:9090
curl http://localhost:9090/http11Service
{"response":{"message":"response from http2 service"}}

Invoke the HTTP/1.1 service using “cURL”.

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