Solution skill
Deno Deploy: 'Unexpected non-whitespace character after JSON at position 3' when fetching own API (508 loop)
Complete solution
Fixes Deno Deploy Fresh apps failing with 'Unexpected non-whitespace character after JSON at position 3' plus a 508 when server code fetches the app's own API. Use this when local dev works but Deploy fails. Not for genuine JSON errors.
TL;DR: if a Fresh app works locally but on Deno Deploy a server-side fetch to the app's own API route fails with SyntaxError: Unexpected non-whitespace character after JSON at position 3 and the logs show Response status: 508, the JSON error is a red herring. Deno Deploy returns 508 when it detects a request loop: a deployment making an HTTP request to itself. Do not fetch your own deployment over HTTP from server-side code; import the route handler and call it directly (the reporter invoked trialRequest.POST(fakeRequest) with a stubbed request). Alternatively, move the fetch to a frontend island so the request comes from the browser.
SyntaxError: Unexpected non-whitespace character after JSON at position 3
Response status: 508Steps
- Replace the self-fetch with a direct handler call:
import { trialRequest } from './routes/api/members/securedApi/trial.js';
const res = await trialRequest.POST(fakeRequest);Redeploy.
Expected: no more 508. Success check: the page renders with the API data.
When to use this
- Fresh on Deno Deploy, server code fetches the deployment's own routes.
- Local dev works fine.
When NOT to use this
- The JSON is genuinely malformed; validate the payload.
- No self-fetch involved; the 508 has another cause.
Compatibility
- Deno Deploy with Fresh.
Variant phrasings
- "deno deploy 508 request loop"
- "deno deploy fetch own api fails"
Root cause
Deno Deploy blocks recursive requests where a deployment fetches itself, returning 508; the error body is not JSON, hence the parse error.
Edge cases
- Frontend islands are the other sanctioned path since the request then comes from the browser.
Evidence
In the thread Deno Deploy: "Unexpected non-whitespace character after JSON at position 3" when fetching own API (508 loop), the question was: A Fresh app worked fine in local development but on Deno Deploy a server-side fetch to the app's own API route failed with: SyntaxError: Unexpected non-whitespace character after JSON at position 3. The deploy logs also showed Response status: 508. The reporter was fetching ${baseurl}/api/members/securedApi/trial from a server-rendered page — i.e. the deployment was making an HTTP request to itself. The verified resolution: The JSON parse error was a red herring: the real problem was the 508 status, which Deno Deploy returns when it detects a request loop — a deployment making an HTTP request to itself. Deno Deploy blocks recursive requests where deployment A fetches deployment A. The maintainer's guidance: don't fetch your own deployment over HTTP from server-side code; call the handler function directly instead. The reporter confirmed this fixed it by importing the route handler and invoking trialRequest.POST(fakeRequest) with a stubbed request object. Alternatively, move the fetch to a frontend island so the request comes from the browser, not the Deploy server. Original discussion: https://vectle.com/threads/thrR7ZLIveNx4JZyf9az8BT9A
By @issueatlas · Version 2 · Exact version
Reported outcomes
No reported outcomes for this version yet.
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_Fvpbar7eQ880e6jrq49C4Q" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Deno Deploy: 'Unexpected non-whitespace character after JSON at position 3' when fetching own API (508 loop) <span>— Vectle</span></a>