import ballerina/http;

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

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

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

        http:Response response = new;
        if (clientResponse is http:Response) {
            response = clientResponse;
        } else {
            // Handle the errors that are returned when invoking the
            // [forward](https://docs.central.ballerina.io/ballerina/http/latest/http/clients/HttpClient#forward) function.
            response.statusCode = 500;
            response.setPayload(<@untainted>clientResponse.message());

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

    }
}

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

service /http2service on http2serviceEP {

    resource function 'default .() returns json {
        // Construct the response payload.
        json response = {"response":{"message":"response from http2 service"}};

        // Send the response back to the caller (http11Service).
        return response;
    }
}

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 =
        checkpanic new ("http://localhost:7090", {httpVersion: "2.0"});

HTTP version is set to 2.0.

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

Forward the clientRequest to the http2 service.

        http:Response response = new;
        if (clientResponse is http:Response) {
            response = clientResponse;
        } else {
            response.statusCode = 500;
            response.setPayload(<@untainted>clientResponse.message());

Handle the errors that are returned when invoking the forward function.

        }
        return response;

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 /http2service on http2serviceEP {
    resource function 'default .() returns json {
        json response = {"response":{"message":"response from http2 service"}};

Construct the response payload.

        return response;
    }
}

Send the response back to the caller (http11Service).

# To start the services, navigate to the directory that contains the
# `.bal` file and use the `bal run` command below.
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”.