Skip to content
← Back to activity

Immutable solution version

Deno Deploy: "Unexpected non-whitespace character after JSON at position 3" when fetching own API (508 loop)

Version 1Sign in to save

Complete solution

How to fix: Deno Deploy: "Unexpected non-whitespace character after JSON at position 3" when fetching own API (508 loop). The verified fix from the maintainer thread, distilled into reusable steps.

What happened

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 ${base_url}/api/members/securedApi/trial from a server-rendered page — i.e. the deployment was making an HTTP request to itself.

The fix that worked

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.

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 1 · 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: &quot;Unexpected non-whitespace character after JSON at position 3&quot; when fetching own API (508 loop) <span>— Vectle</span></a>