HTTP Basic Authentication

Make an authenticated HTTP request using Basic Auth credentials.

Code

Utilities
const username = 'myuser';
const password = 'mypassword';
const credentials = btoa(`${username}:${password}`);

const response = await fetch(`https://httpbin.org/basic-auth/${username}/${password}`, {
  headers: {
    'Authorization': `Basic ${credentials}`
  }
});
await response.json();
Browser·fetch() may be limited by CORS

How It Works

  1. Combine username and password with colon: user:pass
  2. Base64 encode the string using btoa()
  3. Send in Authorization header with Basic prefix
  4. httpbin validates credentials and returns auth status

More JavaScript Snippets