Swan Lake Beta3 is here.
import ballerina/http;
import ballerina/io;

// Defines the HTTP client to call the Basic auth secured APIs.
// The client is enriched with the `Authorization: Basic <token>` header by
// passing the [`http:CredentialsConfig`](https://docs.central.ballerina.io/ballerina/http/latest/records/CredentialsConfig) for the `auth` configuration of the
// client.
http:Client securedEP = check new("https://localhost:9090",
    auth = {
        username: "ldclakmal",
        password: "ldclakmal@123"
    },
    secureSocket = {
        cert: "../resource/path/to/public.crt"
    }
);

public function main() returns error? {
    string response = check securedEP->get("/foo/bar");
    io:println(response);
}

Client - Basic Auth

A client, which is secured with Basic auth can be used to connect to a secured service.
The client is enriched with the Authorization: Basic <token> header by passing the http:CredentialsConfig for the auth configuration of the client.

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

import ballerina/http;
import ballerina/io;
http:Client securedEP = check new("https://localhost:9090",
    auth = {
        username: "ldclakmal",
        password: "ldclakmal@123"
    },
    secureSocket = {
        cert: "../resource/path/to/public.crt"
    }
);

Defines the HTTP client to call the Basic auth secured APIs. The client is enriched with the Authorization: Basic <token> header by passing the http:CredentialsConfig for the auth configuration of the client.

public function main() returns error? {
    string response = check securedEP->get("/foo/bar");
    io:println(response);
}
# As a prerequisite, start a sample service secured with Basic Auth.
# You may need to change the trusted certificate file path.
bal run http_client_basic_auth.bal
Hello, World!
import ballerina/http;
import ballerina/io;

// Defines the HTTP client to call the Basic auth secured APIs.
// The client is enriched with the `Authorization: Basic <token>` header by
// passing the [`http:CredentialsConfig`](https://docs.central.ballerina.io/ballerina/http/latest/records/CredentialsConfig) for the `auth` configuration of the
// client.
http:Client securedEP = check new("https://localhost:9090",
    auth = {
        username: "ldclakmal",
        password: "ldclakmal@123"
    },
    secureSocket = {
        cert: "../resource/path/to/public.crt"
    }
);

public function main() returns error? {
    string response = check securedEP->get("/foo/bar");
    io:println(response);
}

Client - Basic Auth

A client, which is secured with Basic auth can be used to connect to a secured service.
The client is enriched with the Authorization: Basic <token> header by passing the http:CredentialsConfig for the auth configuration of the client.

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

import ballerina/http;
import ballerina/io;
http:Client securedEP = check new("https://localhost:9090",
    auth = {
        username: "ldclakmal",
        password: "ldclakmal@123"
    },
    secureSocket = {
        cert: "../resource/path/to/public.crt"
    }
);

Defines the HTTP client to call the Basic auth secured APIs. The client is enriched with the Authorization: Basic <token> header by passing the http:CredentialsConfig for the auth configuration of the client.

public function main() returns error? {
    string response = check securedEP->get("/foo/bar");
    io:println(response);
}
# As a prerequisite, start a sample service secured with Basic Auth.
# You may need to change the trusted certificate file path.
bal run http_client_basic_auth.bal
Hello, World!
Subscribe to the newsletter

In the creation of Ballerina, we were inspired by so many technologies. Thank you to all that have come before us (and forgive us if we missed one): Java, Go, C, C++, D, Rust, Haskell, Kotlin, Dart, TypeScript, JavaScript, Python, Perl, Flow, Swift, Elm, RelaxNG, NPM, Crates, Maven, Gradle, Kubernetes, Docker, Envoy, Markdown, GitHub and WSO2.

Cookie Policy

This website uses cookies so that we can provide you with the best user experience. Read our Cookie Policy to find out more.

If you wish to disable cookies you can do so from your browser.

I Understand