import ballerina/http;

// An HTTP endpoint can be configured to communicate through HTTPS as well.
// To secure an endpoint using HTTPS, the endpoint needs to be configured with
// a certificate file and a private key file for the endpoint.
// The [secureSocket](https://docs.central.ballerina.io/ballerina/http/latest/http/records/ListenerSecureSocket) record provides the SSL-related listener configurations.
http:ListenerConfiguration helloWorldEPConfig = {
    secureSocket: {
        key: {
            certFile: "../resource/path/to/public.crt",
            keyFile: "../resource/path/to/private.key"
        }
    }
};

// Create a listener endpoint.
listener http:Listener helloWorldEP = new (9095, helloWorldEPConfig);

service /hello on helloWorldEP {

    resource function get .() returns string {
        // Send the response with a string payload back to the caller.
        return "Hello World!";
    }
}

HTTPS Listener

You can use the HTTPS Listener to connect or interact with an HTTPS client. Provide the Secure Socket configuration to the server to expose an HTTPS connection.

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

import ballerina/http;
http:ListenerConfiguration helloWorldEPConfig = {
    secureSocket: {
        key: {
            certFile: "../resource/path/to/public.crt",
            keyFile: "../resource/path/to/private.key"
        }
    }
};

An HTTP endpoint can be configured to communicate through HTTPS as well. To secure an endpoint using HTTPS, the endpoint needs to be configured with a certificate file and a private key file for the endpoint. The secureSocket record provides the SSL-related listener configurations.

listener http:Listener helloWorldEP = new (9095, helloWorldEPConfig);

Create a listener endpoint.

service /hello on helloWorldEP {
    resource function get .() returns string {
        return "Hello World!";
    }
}

Send the response with a string payload back to the caller.

# To start the service, navigate to the directory that contains the
# `.bal` file and execute the `bal run` command along with the Ballerina home path as a config.
# (You may need to change the certificate file path and private key file path.)
bal run https_listener.bal
[ballerina/http] started HTTPS/WSS listener 0.0.0.0:9095
# To invoke the service, execute the cURL command below with the `-k` option.
curl -k https://localhost:9095/hello
Hello World!