Fetch JSON Example code only works with Local Mode enabled

When running the example code below, it works when I enable local mode when running wrangler dev. However, when turn off local mode this same code errors cause of invalid blocked JSON response…

How do you get the fetch.json() to load without local mode enabled?

async function gatherResponse(response) {
    const { headers } = response;
    const contentType = headers.get('content-type') || '';
    if (contentType.includes('application/json')) {
      return JSON.stringify(await response.json());
    }
    return response.text();
}
async function handleRequest() {
    const init = {
        headers: {
            'content-type': 'application/json;charset=UTF-8',
            'user-agent': 'PostmanRuntime/7.29.2',
            'accept': '*/*',
        },
    };
    const response = await fetch('https://api.binance.com/api/v3/ticker/price?symbol=BTCUSDT', init);
    const results = await gatherResponse(response);
    return new Response(results, init);
  }
addEventListener('fetch', event => {
    return event.respondWith(handleRequest());
})

That URL is blocking Workers - there’s nothing you can do other than run it through an external proxy.

Thanks @KianNH

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.