import ballerina/io;
import ballerina/http;
import ballerina/mime;endpoint http:Listener helloWorldEP {
    port:9095,
    secureSocket: {
        keyStore: {
            filePath: "${ballerina.home}/bre/security/ballerinaKeystore.p12",
            password: "ballerina"
        }
    }
};@http:ServiceConfig {
    endpoints:[helloWorldEP],
    basePath:"/hello"
}service<http:Service> helloWorld bind helloWorldEP {
    @http:ResourceConfig {
        methods:["GET"],
        path:"/"
    }    sayHello (endpoint conn, http:Request req) {
        http:Response res = new;
        res.setStringPayload("Successful");
        _ = conn -> respond(res);
    }
}endpoint http:Client clientEP {
    targets: [{
        url: "https://localhost:9095",
        secureSocket: {
            trustStore: {
                filePath: "${ballerina.home}/bre/security/ballerinaTruststore.p12",
                password: "ballerina"
            }
        }
    }]
};
function main (string[] args) {
    http:Request req = new;
    var resp = clientEP -> get("/hello/", req);
    match resp {
        http:HttpConnectorError err => io:println(err.message);
        http:Response response => {
            match (response.getStringPayload()) {
                http:PayloadError payloadError => io:println(payloadError.message);
                string res => io:println(res);
            }
        }
    }
}

HTTPS Server/Client Connectors

This example demonstrates how ballerina https client connector can be configured to connect to a https server through 1-way ssl connection (server is verified by the client). This example uses ballerina https server connector to host a service and https client connector will be sending requests for that server.

import ballerina/io;
import ballerina/http;
import ballerina/mime;
endpoint http:Listener helloWorldEP {
    port:9095,
    secureSocket: {
        keyStore: {
            filePath: "${ballerina.home}/bre/security/ballerinaKeystore.p12",
            password: "ballerina"
        }
    }
};
@http:ServiceConfig {
    endpoints:[helloWorldEP],
    basePath:"/hello"
}
service<http:Service> helloWorld bind helloWorldEP {
    @http:ResourceConfig {
        methods:["GET"],
        path:"/"
    }
    sayHello (endpoint conn, http:Request req) {
        http:Response res = new;
        res.setStringPayload("Successful");
        _ = conn -> respond(res);
    }
}
endpoint http:Client clientEP {
    targets: [{
        url: "https://localhost:9095",
        secureSocket: {
            trustStore: {
                filePath: "${ballerina.home}/bre/security/ballerinaTruststore.p12",
                password: "ballerina"
            }
        }
    }]
};
function main (string[] args) {

Ballerina client connector can be used to connect to the created https server. You have to run the service before running this main function. As this is a 1-way ssl connection, client needs to provide trustStoreFile and trustStorePassword.

    http:Request req = new;
    var resp = clientEP -> get("/hello/", req);
    match resp {
        http:HttpConnectorError err => io:println(err.message);
        http:Response response => {
            match (response.getStringPayload()) {
                http:PayloadError payloadError => io:println(payloadError.message);
                string res => io:println(res);
            }
        }
    }
}

Creates an outbound request.

$ ballerina run https-server-client-connectors.bal
ballerina: started HTTPS/WSS endpoint 0.0.0.0:9095
Successful
ballerina: stopped HTTPS/WSS endpoint 0.0.0.0:9095