HTTP Basic Authentication is one of the simplest authentication schemes defined in the HTTP specification. The client sends a username and password, combined and Base64-encoded, in the Authorization header. The header format is Authorization: Basic <base64(username:password)>. Because the credentials are only encoded (not encrypted), Basic Auth should only be used over HTTPS.
HttpClientRequest.basicAuth() handles the encoding for you:
import { FetchHttpClient, HttpClient, HttpClientRequest,} from "effect/unstable/http";import { Effect } from "effect";
function fetchWithBasicAuth() { return Effect.gen(function* () { const client = yield* HttpClient.HttpClient;
const request = HttpClientRequest.get( "https://httpbin.org/basic-auth/admin/secret123", ).pipe(HttpClientRequest.basicAuth("admin", "secret123"));
const response = yield* client.execute(request); const data = yield* response.json;
return data; }).pipe(Effect.provide(FetchHttpClient.layer));}
// Test itEffect.runPromise(fetchWithBasicAuth()).then((data) => { console.log("Auth result:", data);});Output:
Auth result: { authenticated: true, user: 'admin' }basicAuth(username, password) Base64-encodes the credentials and sets the Authorization header to Basic <encoded>. You never need to handle the encoding yourself.