import ballerina/io;
import ballerina/websocket;
// Defines the WebSocket client to call the Basic auth secured APIs.
// The client is enriched with the `Authorization: Basic <token>` header by
// passing the [`websocket:CredentialsConfig`](https://docs.central.ballerina.io/ballerina/websocket/latest/records/CredentialsConfig) for the `auth` configuration of the
// client.
websocket:Client securedEP = check new("wss://localhost:9090/foo/bar",
auth = {
username: "ldclakmal",
password: "ldclakmal@123"
},
secureSocket = {
cert: "../resource/path/to/public.crt"
}
);
public function main() returns error? {
check securedEP->writeTextMessage("Hello, World!");
string textMessage = check securedEP->readTextMessage();
io:println(textMessage);
}
Client - Basic AuthA client, which is secured with Basic auth can be used to connect to
a secured service. |
import ballerina/io;
import ballerina/websocket;
websocket:Client securedEP = check new("wss://localhost:9090/foo/bar",
auth = {
username: "ldclakmal",
password: "ldclakmal@123"
},
secureSocket = {
cert: "../resource/path/to/public.crt"
}
);
Defines the WebSocket client to call the Basic auth secured APIs.
The client is enriched with the Authorization: Basic <token>
header by
passing the websocket:CredentialsConfig
for the auth
configuration of the
client.
public function main() returns error? {
check securedEP->writeTextMessage("Hello, World!");
string textMessage = check securedEP->readTextMessage();
io:println(textMessage);
}
# As a prerequisite, start a sample echo WebSocket service secured with Basic Auth.
# You may need to change the trusted certificate file path.
bal run websocket_client_basic_auth.bal
Hello, World!