October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

Why “Unexpected token <” Usually Means Your API Returned HTML

“Unexpected token

Unexpected token '<' usually means JavaScript tried to parse a response as JSON, but the response began with HTML markup instead. The error points to a mismatch between the format your code expected and the body it received; by itself, it does not tell you which server, route, proxy, or other component supplied that HTML.

What the error means

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

JSON.parse() accepts text that follows JSON grammar. If the text does not, JavaScript throws a SyntaxError; Response.json() fails too when the response body cannot be parsed as JSON. A reported < often points to a body that starts with an HTML doctype or tag, such as an error page or a frontend page returned in place of API data. MDN’s JSON.parse() documentation describes the parsing requirement, while its guide to Unexpected token errors explains that the message alone does not identify a unique cause.

This is a clue, not proof that a particular API server generated the HTML. The body might have come from routing, authentication or redirect handling, a frontend fallback, a proxy or gateway, or a server error handler.

Why fetch can succeed while JSON parsing fails

A fulfilled fetch() promise means a Response was received; it does not guarantee an HTTP success status or a JSON body. For example, an HTTP 404 still produces a response. MDN’s Using the Fetch API guide advises checking the response status as well as handling fetch errors.

So there are two separate questions: did the request receive an acceptable HTTP status, and does its body match the format the code expects? Check response.ok or response.status for the first, and the response’s Content-Type and body for the second.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How to find where the HTML came from

  1. Find the request. In your browser’s Network panel, select the failing request and confirm its URL and method. Make sure it is the API endpoint you intended to call.
  2. Check the status and final URL. A 404 or another non-OK status may point to a wrong route or server-side error. A changed final URL can help reveal redirect handling, such as a request ending at a sign-in or frontend page.
  3. Check Content-Type. If it is not a JSON media type, do not assume response.json() can parse the body as JSON. Some JSON APIs use vendor media types, including application/problem+json, so account for the media types your API documents rather than checking only for one exact string.
  4. Preview the body as text. A short preview can show whether the response is an HTML page, a plain-text error, or another representation. Avoid logging sensitive response bodies in production.
  5. Trace the layer that returned it. Use the URL, status, headers, and body together to investigate routing, authentication or redirect handling, a frontend fallback, a proxy or gateway, or a server error handler. The parse error alone cannot distinguish among these possibilities.

Handle HTTP errors and unexpected bodies separately

This illustrative pattern checks the HTTP status and media type before parsing. It reads the body as text once so it can include a short preview in an error; a response body cannot be consumed twice, so the success path parses that saved text rather than calling response.json() afterward.

async function getJson(url) {
  const response = await fetch(url);
  const contentType = response.headers.get("content-type") ?? "";
  const body = await response.text();

  if (!response.ok) {
    throw new Error(`HTTP ${response.status} for ${url}`);
  }

  const isJson = /(?:application|[^;]++json)/i.test(contentType);
  if (!isJson) {
    const preview = body.slice(0, 200);
    throw new TypeError(`Expected JSON, received ${contentType}: ${preview}`);
  }

  return JSON.parse(body);
}

This is a starting point, not a universal API client. Adapt media-type checks and error handling to your API, and redact secrets before placing body previews in logs or error reports. Keep HTTP-status failures distinct from JSON syntax failures: the former indicates an unsuccessful response, while the latter means the body could not be parsed as JSON.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What will not fix it

Changing JSON parsing options cannot turn an HTML error page into the API data your application intended to receive. First correct the request path or the server-side behavior that returned the wrong representation; then parse the response that endpoint is meant to provide.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.