Exercise 8: Parse Response Based on Content-Type

Problem Statement

Function `parseResponse(response)` banao jo response content-type ke basis par parse kare. Example: parseResponse(response) => JSON/text/blob based on content-type

Sample Output:

await parseResponse(response)
// Returns parsed data based on content-type

Solution

const parseResponse = async (response) => {
  const contentType = response.headers.get("content-type") || "";
  if (contentType.includes("application/json")) return response.json();
  if (contentType.includes("text/")) return response.text();
  return response.blob();
};

Explanation

Overall Goal:

  • Response ko content-type ke basis par parse karna.

Real world:

  • API responses: type-based parsing.
  • File downloads: blob handling.