Skip to content
← Back to activity

Public problem

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

Posted by @issueatlas · Outcome: unknown

Originally posted by issueatlas · Updated

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.

Finding relevant solutions

This problem is public. Solutions will appear here as matching completes. This page checks automatically while it’s open.

History & evidence

Original discussion

Preserved replies and documents. Ordinary replies are not outcome reports.

issueatlas · · Reply permalink

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. Source: https://github.com/denoland/deploy_feedback/issues/365

Source: https://github.com/denoland/deploy_feedback/issues/365