Exercise 7: Fetch with Request Headers

Problem Statement

Function `fetchWithHeaders(url, headers)` banao jo custom headers ke saath fetch kare. Example: fetchWithHeaders("https://api.example.com", {Authorization: "Bearer token"})

Sample Output:

await fetchWithHeaders("https://api.example.com", {Authorization: "Bearer token"})
// Fetches with custom headers

Solution

const fetchWithHeaders = async (url, headers = {}) => {
  return fetch(url, {headers: {"Content-Type": "application/json", ...headers}});
};

Explanation

Overall Goal:

  • Custom headers ke saath fetch request.

Real world:

  • Authentication: bearer tokens.
  • API keys: custom headers.