Accessing Protected Routes
How to access routes secured with HTTP Basic Authentication in TezX.
This guide demonstrates how to access routes secured with HTTP Basic Authentication using various tools and libraries.
Using cURL
Option 1: With -u flag (recommended)
curl -u username:password http://localhost:3000/protectedOption 2: Manually set the Authorization header
curl -H "Authorization: Basic $(echo -n 'username:password' | base64)" http://localhost:3000/protected✅ The -u flag automatically encodes credentials in Base64.
Using Postman
- Open a new request in Postman.
- Navigate to the Authorization tab.
- Select Basic Auth as the type.
- Enter your
usernameandpassword. - Click Send.
Postman automatically encodes and adds the Authorization header.
Using JavaScript (Fetch API)
const username = "admin";
const password = "password123";
fetch("http://localhost:3000/protected", {
method: "GET",
headers: {
Authorization: `Basic ${btoa(`${username}:${password}`)}`,
},
})
.then((res) => res.json())
.then((data) => console.log(data))
.catch((err) => console.error("Error:", err));🔍 btoa() encodes credentials in Base64 format.
Using Axios
const axios = require("axios");
axios
.get("http://localhost:3000/protected", {
auth: {
username: "admin",
password: "password123",
},
})
.then((res) => console.log(res.data))
.catch((err) => console.error("Error:", err));💡 The auth option in Axios handles Base64 encoding internally.