Exercise 17: Fetch with Authentication Token
Problem Statement
Function `fetchWithAuth(url, token)` banao jo Authorization header ke saath fetch kare.
Example: fetchWithAuth("https://api.example.com", "Bearer token123")
Sample Output:
await fetchWithAuth("https://api.example.com", "token123")
// Fetches with Authorization headerSolution
const fetchWithAuth = async (url, token) => {
return fetch(url, {
headers: {Authorization: `Bearer ${token}`}
});
};Explanation
Overall Goal:
- Authentication token ke saath fetch.
Real world:
- API authentication: bearer tokens.
- Secure requests: token-based auth.