You call response.json() and get this:
SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON The message is honest. The first character of the body is <, so the server sent an HTML page, and your code tried to read it as JSON. The JSON parser is not the problem. The real question is which page you got and why.
Look at what actually arrived
Before changing any code, look at the raw response. Two ways take a few seconds.
Browser. DevTools, Network tab, click the request. Check three things: the status code, the Content-Type header and the Response tab. If you see text/html and page markup, you have your answer.
curl.
curl -i https://your-site.example/api/orders | head -20 -i prints the status line and headers before the body. The <title> of the page usually tells you who sent it: your frontend, nginx, a login page, a CDN.
Six places the HTML comes from
| What you see | Where it came from | Fix |
|---|---|---|
| 404 page of your site | Wrong path or base URL, a missing /api prefix | Check the URL and the environment variable with the API address |
Your own index.html | Dev server or SPA fallback answered instead of the API | Proxy /api to the backend (see below) |
| Login page, status 200 | Session expired and you were redirected | Check auth, handle 401 and redirects before parsing |
nginx 502 Bad Gateway page | Backend is down or the upstream address is wrong | Check that the backend is running and reachable from the proxy |
| CDN or WAF challenge page | Protection blocked an automated request | Allow the client, or call the API host directly in tests |
| Maintenance page | Deploy in progress | Retry later, and make the API return JSON 503 instead |
The two most common ones in practice are the dev server and the SPA fallback, because they return status 200 and look like success.
Case 1: it only happens in local development
The frontend runs on one port, the API on another. A request to /api/orders goes to the frontend dev server, which does not know that path and returns index.html. In Vite the fix is a proxy:
// vite.config.js export default { server: { proxy: { '/api': 'http://localhost:8080' } } } Restart the dev server after the change. Other bundlers have the same setting, also called proxy.
Case 2: it happens on the server
In production the same thing is done by a rule that serves index.html for any unknown path. Single-page apps need it, but it must not catch the API:
location /api/ { proxy_pass http://backend:8080; } location / { try_files $uri /index.html; } If /api/ has no own location, every API call that the backend does not serve ends up as your app's HTML with status 200.
Guard the parse in code
Check the status and the content type before calling json(). Then instead of a confusing SyntaxError you get an error that says what really happened:
const res = await fetch(url); const type = res.headers.get('content-type') || ''; if (!res.ok || !type.includes('application/json')) { const text = await res.text(); throw new Error(`HTTP ${res.status}, ${type}: ${text.slice(0, 200)}`); } const data = await res.json(); The first 200 characters of the body are usually enough to recognise the page.
Catch it in API tests
A test that only checks status == 200 passes on a login page or on index.html. Add two cheap checks to every API test:
-
Content-Typecontainsapplication/json; - one field you really expect is present, for example
idorstatus.
That way the test fails with a clear reason the moment routing breaks, instead of the next step failing with a parse error.
If the body really is JSON and it still fails
Then it is a different error: a trailing comma, single quotes, a BOM at the start of a file, or a truncated response. A JSON validator shows the exact line and column. We collected the variants with the messages from JavaScript, Python and Java here:
- the full guide to this error: https://checkcraft.ru/en/guides/json-doctype-not-valid.html?utm_source=devto
- other
Unexpected tokencases: https://checkcraft.ru/en/guides/json-unexpected-token.html?utm_source=devto - a validator that points to the line: https://checkcraft.ru/en/guides/json-validator-online.html?utm_source=devto
We make Checkcraft, a desktop app that checks API responses and the matching log records, and this error is one of the first things it flags. Everything above works without it.
Which of the six sources bit you last time?
Written with AI assistance and checked by a human.


