© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Cloudflare DevelopersCD
Cloudflare Developers•8mo ago•
6 replies
Senti3ntB3ing

fetch not working

fetch is not working in my program, going to attempt to show it as well as possible.

the initial call :

async chat(message) {
    console.log(message);
    let payload = {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + TWITCH_APP_TOKEN,
            "Client-Id": TWITCH_APP_ID
        },
        body: {
            "broadcaster_user_id": "12341234",
            "sender_id": "12341234",
            "message": message,
            "for_source_only": "true"
        }
    };
    let url = "http://api.twitch.tv/helix/chat/messages";
    let response = await fetch(url, payload);

    if (!response.ok) {
        return Response.json({
            status: response.status,
            error: 'oauth error?'
        }, {
            status: 500
        });
    }
    if (!response.headers?.get('content-type')?.includes('application/json')) {
        return Response.json({
            status: response.status,
            error: 'malformed json'
        }, {
            status: 500
        });
    }
    const json = await response.json();
    console.log(json);
    return
}
async chat(message) {
    console.log(message);
    let payload = {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + TWITCH_APP_TOKEN,
            "Client-Id": TWITCH_APP_ID
        },
        body: {
            "broadcaster_user_id": "12341234",
            "sender_id": "12341234",
            "message": message,
            "for_source_only": "true"
        }
    };
    let url = "http://api.twitch.tv/helix/chat/messages";
    let response = await fetch(url, payload);

    if (!response.ok) {
        return Response.json({
            status: response.status,
            error: 'oauth error?'
        }, {
            status: 500
        });
    }
    if (!response.headers?.get('content-type')?.includes('application/json')) {
        return Response.json({
            status: response.status,
            error: 'malformed json'
        }, {
            status: 500
        });
    }
    const json = await response.json();
    console.log(json);
    return
}


the method that calls that method:

handler(message) {
  if (message === undefined) return;
// for (const heart in #hearts) if (message.startsWith(heart)) {
//     this.#commands.announce(message.substring(1).trim(), hearts[heart]);
//     return;
// }
  this.chat(message);
};
handler(message) {
  if (message === undefined) return;
// for (const heart in #hearts) if (message.startsWith(heart)) {
//     this.#commands.announce(message.substring(1).trim(), hearts[heart]);
//     return;
// }
  this.chat(message);
};

basically the logic set up is like this:

export default{
  async fetch(request, env, ctx){
  // constants declared, blah blah
  class Database{} // uses async fetch here too
  class TwitchChat{} // this is the class that contains those methods
  switch(request.method){} // switch on the method, with switch on pathnames as well within
  }
}
export default{
  async fetch(request, env, ctx){
  // constants declared, blah blah
  class Database{} // uses async fetch here too
  class TwitchChat{} // this is the class that contains those methods
  switch(request.method){} // switch on the method, with switch on pathnames as well within
  }
}

@James here is more of the logic, i copy pasted the code you used and that didn't seem to work so not sure what the issue could be
Cloudflare Developers banner
Cloudflare DevelopersJoin
Welcome to the official Cloudflare Developers server. Here you can ask for help and stay updated with the latest news
85,042Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

fetch() not working
Cloudflare DevelopersCDCloudflare Developers / workers-and-pages-help
2y ago
Workers fetch cache not working?
Cloudflare DevelopersCDCloudflare Developers / workers-and-pages-help
3y ago
early return on fetch not working
Cloudflare DevelopersCDCloudflare Developers / workers-and-pages-help
2y ago
POST method is not working in fetch inside Queue Handler
Cloudflare DevelopersCDCloudflare Developers / workers-and-pages-help
13mo ago