Stream webhook request body was not parsable

/**
 * Subscribes to broadcast changes for the specified broadcaster.
 * @param {String} broadcasterId Unique id of broadcaster to monitor.
 */
function singleStreamWebhook(broadcasterId){

   $.ajax({
        type: "POST",
        url: "https://api.twitch.tv/helix/webhooks/hub",
        headers: {
            "Client-ID": json.clientId,
            "Content-Type": "application/json"
        },
        data: {
            "hub.callback": `https://MyDomain.me/callbackPath/${broadcasterId}`,
            "hub.mode": "subscribe",
            "hub.topic": `https://api.twitch.tv/helix/streams?user_id=${broadcasterId}`,
            "hub.lease_seconds": 864000,
            "hub.secret": json.personalSecret
        },
    });
}

Where:

  • json.personalSecret is something made through mashing a bunch of keys on my keyboard (not encoded)
  • The callback path ends with the broadcaster’s id so I can find out who went offline/online by parsing the url.

This code gets me:

{"error":"Bad Request","status":400,"message":"Request body was not parsable. Attempted Content-Type: \"application/json\""}

Can someone tell me what I’m doing wrong? Thanks!

Why is this call being made in Ajax.

Ajax suggests that this code is being performed via a front end which means you are leaking your hub.secret to the world…

I imagine the Ajax call with conten-type application/json is trying to be clever and is converting the data to JSON before sending it. As you declared (via the header) that you wish to send JSON. (Content-Type says I’m sending this, Accept is I expect that)

Try body instead of data or pass the arguments/data (url encoded) via querystring instead of post body.

I don’t post any body I pass by querystring:

    topic = encodeURIComponent(topic);

    var theurl = 'https://api.twitch.tv/helix/webhooks/hub'
                + '?hub.callback=' + callback
                + '&hub.mode=subscribe'
                + '&hub.topic=' + topic
                + '&hub.lease_seconds=' + lease
                + '&hub.secret=' + config.hook_secret;
2 Likes

Sorry, I’m new to JavaScript. I’m pretty much learning it by making this extension. What would I use instead of ajax?

Any server side request technology.

Javascript would infer node, node offers doing it raw or modules such as

  • request
  • axios

I prefer request myself

Since you are building an extension you can use any server language you want to build your EBS and capture webhook data. The Extension itself cannot receive the webhook data directly

1 Like

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