Node Red - Webhook

Hello,
I want to use the Webhooks in Node-red. But for this to be used i need the payload as JSON.
I tried setting up a webhook with the http in node, a debug node and a http response node.
But the result i get is not parsed as JSON.
Can anyone setup a example Node red flow that parses the payload of the post to readable JSON format.?

Hi,
It seems that this implementation doesn’t work because the POST is multi part body.
What I’ve tested and works is to use this node httpInMultipart
The response of this node can be afterwards processed like this:
json = JSON.parse(msg.payload.payload);

ah oke… what did you fill in the fields input field?

I’ve tried several things, but in the end I put only:
[]

Yes that works partly. I got the message: “Error: Unexpected field” when media.play is send in debug panel. But for the media.stop event i see the whole json in the debug panel.

You’re right, I didn’t observe it.
I don’t know if this is an issue from Plex or with the node.

My colleague came up with this solution, works for me:

var json = msg.payload.toString();
var startPosition = json.indexOf(’{’);
json = json.substr(startPosition);

var endPosition = json.indexOf("
–");
json = json.substr(0, endPosition);

msg.payload = json;

return msg;

This is it. Thanks

Hi,

I’m trying to get the Webhooks of Plex working with Node-Red but cannot for the life of me get it to work.

Here’s my setup:

Plex server is installed on my Windows 10 PC.
Node-Red is installed on a Beaglebone Black (BBB).

In the plex settings I have set the Webhook to point the IP address of the BBB including various ports/folders etc like:

http://192.168.1.251:12000
http://192.168.1.251:12000/plex
http://192.168.1.251/plex
http://192.168.1.251

and have tried multiple configurations of the http in node and the Websocket in node in Node-Red but without success.

Can someone point to where I may be going wrong please.

Thanks

Hi,

My implementation looks like below:


So ‘/plex’ is enough for URL and set ‘POST’ for Method.

But before that you can try something simpler, make an additional flow, we’re you have an ‘http in’, connected to ‘http respone’ and ‘debug’. For the ‘http in’ set ‘GET’ and ‘/test’.
Now, from a different device try the ‘curl’ command; something like this ‘curl http://192.168.1.251:12000/test’. The link used should have the same port as the one you use to access Node-red
You will have more chances for debugging.
It could be that the link is password protected, if in Node-Red you activated ‘httpNodeAuth’.

Thanks for that, it was the Node-Red port number that I needed to put in there.

All working now :slight_smile:

I realize this is a bit old now but just throwing in my 2 cents. On the http input node I set it to POST and Accept File Uploads (this feature may have been added since the original post), I pass this into a function node with these two lines in it (very similar to how iura.alin) . . .

msg.payload = JSON.parse(msg.payload.payload);
return msg;

Also @groovygrovesy 's the url’s in node-red are relative to your base url, if you access node-red via http://localhost:21000/red and create a node called /plex then it’s full url is http://localhost:21000/red/plex I realize your issue is resolved but just in case it wasn’t clear to other people who find this post.

I would be very interested to know what people have come up with, I saw the suggestion on the Plex website about dimming the lights when you play a movie, sounds great, anyone got a IoT popcorn maker?

No Iot popcorn maker for me, just light automation when I play a movie… :wink: But it’s seems that the webhook are very slow from plex. IThey are appearing sometimes many seconds after in node red…
Did you have the same problem?

Hi, I’m trying to following topic and emulate the steps. Any tutorial for me to follow step by step to create each node?

I’ve being able to setup this using but would like to use node red due to lack of support in homeassistant for node.js.

The idea is to get webhook from plex and trigger light automation.

var express = require('express')
  , request = require('request')
  , multer  = require('multer');

var app = express();
var upload = multer({ dest: '/tmp/' });

app.post('/', upload.single('thumb'), function (req, res, next) {
  var payload = JSON.parse(req.body.payload);
  console.log('Got webhook for', payload.event);

 // USER INPUT: You only need the comment below once to get the UUID. Play a movie on the client you want to automate and run the script. You can comment out it when you got the UUID.
   console.log(payload.Player.uuid)


  // USER INPUT: Insert the UUID you got from the console instead of 'PLAYER-UUID'
 if (payload.Player.uuid == 'PLAYER-UUID' && payload.Metadata.type != 'track') {
	var options = {
      method: 'POST',
      json: true,
	  url: "http://192.168.1.2:8123/api/states/sensor.plex_sensor", 
	  headers: { Authorization: 'Bearer ' }
    };

    if (payload.event == 'media.play' || payload.event == 'media.resume') {
      console.log('Plex client playing');
      if (payload.Metadata.type == 'episode'){
      options.body = {'state':'Playing','attributes':{'series':payload.Metadata.grandparentTitle,'title':payload.Metadata.title,'season':payload.Metadata.parentIndex,'episode':payload.Metadata.index,'client':payload.Player.title, 'mediatype':payload.Metadata.type,'user':payload.Account.title}};
      }
      else{
      options.body = {'state':'Playing','attributes':{'title':payload.Metadata.title, 'client':payload.Player.title, 'mediatype':payload.Metadata.type, 'user':payload.Account.title }};
      }
      request(options);
    } else if (payload.event == 'media.pause') {
      console.log('Plex client paused');
      options.body = {'state':'Paused'};
      request(options);
    } else if  (payload.event == 'media.stop'){
      console.log('Plex client stopped');
      options.body = {'state':'Stopped'};
      request(options);
    }
  }

  res.sendStatus(200);
});
// USER INPUT: Choose the port of your liking.
app.listen(12000);

I’m still seeing slow webhook posts from plex too, did this ever get solved?

No I didn’t get solved, I dropped this solution which is unstable

Hi, don’t mean to revive an old ish thread, but I have been trying to get an http in node in nodered to accept a local webhook from plex, but I can’t figure out how to get it to work.

I’ve got Plex posting webhooks to 192.168.1.33:1880/Plex

That ip address being the IP address for my home assistant virtual machine which has the nodered add-on running. I’ve got an http in node in post mode with the accept file downloads checked, but nothing is coming in. I have it set to watch /plex.

Any ideas what’s wrong?

I’ve Just been playing with this and managed to get it to work - here’s a test flow you can import and try…

[{"id":"17b0ecc7.aaa9e3","type":"tab","label":"Plex test","disabled":false,"info":""},{"id":"3cab80ec.d92ae","type":"debug","z":"17b0ecc7.aaa9e3","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":510,"y":260,"wires":[]},{"id":"d159aee8.6e503","type":"http in","z":"17b0ecc7.aaa9e3","name":"Plex WebHook","url":"/plex","method":"post","upload":true,"swaggerDoc":"","x":210,"y":260,"wires":[["94b7b65f.227a08"]]},{"id":"94b7b65f.227a08","type":"function","z":"17b0ecc7.aaa9e3","name":"Parse","func":"msg.payload = JSON.parse(msg.payload.payload);\nreturn msg;","outputs":1,"noerr":0,"x":440,"y":420,"wires":[["3cab80ec.d92ae","807a187d.5d2478","4c8ceb11.1a9f84","c7907c52.36c1c"]]},{"id":"807a187d.5d2478","type":"debug","z":"17b0ecc7.aaa9e3","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.event","targetType":"msg","x":650,"y":340,"wires":[]},{"id":"4c8ceb11.1a9f84","type":"debug","z":"17b0ecc7.aaa9e3","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.Account.title","targetType":"msg","x":670,"y":380,"wires":[]},{"id":"c7907c52.36c1c","type":"switch","z":"17b0ecc7.aaa9e3","name":"","property":"payload.event","propertyType":"msg","rules":[{"t":"eq","v":"media.play","vt":"str"},{"t":"eq","v":"media.resume","vt":"str"},{"t":"eq","v":"media.pause","vt":"str"},{"t":"eq","v":"media.stop","vt":"str"}],"checkall":"true","repair":false,"outputs":4,"x":610,"y":480,"wires":[[],[],[],[]]}]

Thanks for the suggestion and reply. After a while I came to the conclusion that I would have to open the port for plex on my router to get the webhooks to work. I instead decided to just move to an “addon” for node red that gets the information directly.

I appreciate you posting though!