Plex webhook handler request.body sends back {}

Hello Guys. I’m opening this topic because I haven’t found any usable topics about this problem yet.
I’m running node.js with the script below, and when I activate an event on plex, I get a pair of curly braces back (empty JSON, I think), logging request.body without any information.

Code:
var express = require(‘express’)
, request = require(‘request’)
, bodyParser = require(‘body-parser’);

var app = express();
app.use(bodyParser.urlencoded({ extended: true }))

app.use(bodyParser.json());

app.post(’/’, function (req, res) {

console.log(req.body);

res.sendStatus(200);

});

app.listen(1000);

Can anyone help please

I’m having the same issue. Both plex and node server on the same host. I’m definitely getting a POST request but it is coming in with an empty body. You ever sort this one out?

edit:
nvm pretty sure I figured this out. body-parser urlencoded doesn’t work for for multi-part form data, which is the content type for Plex Webhooks. You need to use a different parser. See here: https://www.npmjs.com/package/body-parser#readme They recommend you use something like formidible or multer

I still can’t solve it, so I’m using PHP. I can post the code if you want to use it as well. It works perfectly. The olny problem is that it’s slow, perhaps because I’m using ZWave devices.

edit: Thanks, I’ll try with multer!

ahh cool. I ended up using busboy actually. Seems to work pretty good. I used it like so:

import Busboy from 'busboy'
import express from 'express'
const router = new express.Router();

router.post('/plex', async function(req, res, next) {
    const busboy = new Busboy({ headers: req.headers });
    let payload = null;
    busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
        file.resume(); // don't care about saving the poster
    });
    busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated, encoding, mimetype) {
        if (fieldname === 'payload') {
            try {
                payload = JSON.parse(val);
            } catch (e) {
                log.info(e);
            }
        }
    });
    busboy.on('finish', async function() {
        if (payload) {
            if (payload.event === 'media.play'){
                const webhook = new IncomingWebhook(slackConfig.webhookURL);
                const msg = {
                    text: `Server ${payload.Server.title} is playing the following ${payload.Metadata.type} ${payload.Metadata.title}`
                };
                await webhook.send(msg);
                log.info(`sent a message to slack!:${msg}`);
            } else {
                log.info(`noop for ${payload.event}`);
            }
        }
        res.writeHead(303, { Connection: 'close', Location: '/' });
        res.end();
    });
    return req.pipe(busboy);
});

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