import ballerina/auth;
import ballerina/http;
import ballerina/log;
auth:OutboundBasicAuthProvider outboundBasicAuthProvider = new({
    username: "tom",
    password: "1234"
});
http:BasicAuthHandler outboundBasicAuthHandler =
                                            new(outboundBasicAuthProvider);http:Client httpEndpoint = new("https://localhost:9090", {
    auth: {
        authHandler: outboundBasicAuthHandler
    },
    secureSocket: {
        trustStore: {
            path: "${ballerina.home}/bre/security/ballerinaTruststore.p12",
            password: "ballerina"
        }
    }
});public function main() {
    var response = httpEndpoint->get("/hello/sayHello");
    if (response is http:Response) {
        var result = response.getTextPayload();
        log:printInfo((result is error) ? "Failed to retrieve payload."
                                        : result);
    } else {
        log:printError("Failed to call the endpoint.", response);
    }
}import ballerina/auth;
import ballerina/http;
import ballerina/log;
auth:InboundBasicAuthProvider inboundBasicAuthProvider = new;
http:BasicAuthHandler inboundBasicAuthHandler = new(inboundBasicAuthProvider);listener http:Listener ep  = new(9090, config = {
    auth: {
        authHandlers: [inboundBasicAuthHandler],
        scopes: ["hello"]
    },
    secureSocket: {
        keyStore: {
            path: "${ballerina.home}/bre/security/ballerinaKeystore.p12",
            password: "ballerina"
        }
    }
});service hello on ep {
    resource function sayHello(http:Caller caller, http:Request req) {
        error? result = caller->respond("Hello, World!!!");
        if (result is error) {
            log:printError("Error in responding to caller", result);
        }
    }
}

Secured Client with Basic Auth

A client, which is secured with Basic authentication should be used to connect to a service, which is secured with Basic authentication. The auth:OutboundBasicAuthProvider is initialized with the username and password and the http:BasicAuthHandler is initialized by providing the created auth:OutboundBasicAuthProvider. An additional auth field is added to the HTTP client endpoint initialization in order to secure the simple HTTP client endpoint. The authHandler field is defined inside the auth field with the value of it being the reference of the created http:BearerAuthHandler.

import ballerina/auth;
import ballerina/http;
import ballerina/log;
auth:OutboundBasicAuthProvider outboundBasicAuthProvider = new({
    username: "tom",
    password: "1234"
});

Defines the Basic Auth client endpoint to call the backend services. Basic Authentication is enabled by creating an auth:OutboundBasicAuthProvider with the username and password passed as a record.

http:BasicAuthHandler outboundBasicAuthHandler =
                                            new(outboundBasicAuthProvider);

Creates a Basic Auth handler with the created Basic Auth provider.

http:Client httpEndpoint = new("https://localhost:9090", {
    auth: {
        authHandler: outboundBasicAuthHandler
    },
    secureSocket: {
        trustStore: {
            path: "${ballerina.home}/bre/security/ballerinaTruststore.p12",
            password: "ballerina"
        }
    }
});
public function main() {
    var response = httpEndpoint->get("/hello/sayHello");
    if (response is http:Response) {
        var result = response.getTextPayload();
        log:printInfo((result is error) ? "Failed to retrieve payload."
                                        : result);
    } else {
        log:printError("Failed to call the endpoint.", response);
    }
}

Send a GET request to the specified endpoint.

# To start the service, navigate to the directory that contains the
# `.bal` file and use the `ballerina run` command.
$ ballerina run secured_client_with_basic_auth.bal
[ballerina/http] started HTTPS/WSS listener 0.0.0.0:9090
INFO  [ballerina/log] - Hello, World!!!
import ballerina/auth;
import ballerina/http;
import ballerina/log;
auth:InboundBasicAuthProvider inboundBasicAuthProvider = new;
http:BasicAuthHandler inboundBasicAuthHandler = new(inboundBasicAuthProvider);

Defines the sample backend service, which is secured with Basic Auth authentication.

listener http:Listener ep  = new(9090, config = {
    auth: {
        authHandlers: [inboundBasicAuthHandler],
        scopes: ["hello"]
    },
    secureSocket: {
        keyStore: {
            path: "${ballerina.home}/bre/security/ballerinaKeystore.p12",
            password: "ballerina"
        }
    }
});
service hello on ep {
    resource function sayHello(http:Caller caller, http:Request req) {
        error? result = caller->respond("Hello, World!!!");
        if (result is error) {
            log:printError("Error in responding to caller", result);
        }
    }
}