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 switchIn 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. |
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
curl http://localhost:9090/http11Service
{"response":{"message":"response from http2 service"}}
Invoke the HTTP/1.1 service using “cURL”.