Hi I really need some help with this one. I have a security system I want to do a http request to. It's some kind of a connection that stays open and update the events if I use for example chrome. It's an xml document. I have tried the regular http request node but it does not connect at all. I tried the multipart decoder and it succseeded to get the latest event and then the the node says (no multipart url) and it don't get any more updates if I don't inject it again. Hope you understand what I'm trying to accomplish.
I guess websockets or ajax is used to update the client web page but it not possible to know without more info. Otherwise how would the server be able to send events back just with a http/https connection?
Hi Niclas,
My multipart decoder node sees that the response content-type is not multipart, so it will stop. My node-red-contrib-sse-client will most probably also not work, since your response content type doesn't contain "event-stream".
Do you have any extra info about the type of connection?
Bart
This is chunked transfer encoding which is the default encoding. The http-request node will (see code) wait until all chunks have arrived (via the "request" npm library), and then send the entire response body as an output message. But could it perhaps be that your system keeps sending chunks, so there is no end of the response. Which means that the http request node never sends an output message... Could it work like that? I mean that every chunk is in your case in fact a complete piece of data.
If so, the second example here could perhaps be of any help to you. Suppose you rewrite it a bit like this (out of my head, so not tested!):
const request = require('request')
request(
{ method: 'GET'
, uri: 'http://192.168.80.121/arx/eventexport?end_date=keep'
, gzip: true
}
, function (error, response, body) {
console.log('Most probably the stream has been ended ...')
}
)
.on('data', function(data) {
// decompressed data as it is received
console.log('decompressed chunk arrived: ' + data)
node.send({payload: data});
})
The .on('data ...' function should be called every time a chunk arrives.
When you use this in a function node, don't forget to add the request library to the settings.js file (as described here). See also this discussion...
So instead of waiting for the entire response to arrive, the function node will send an output message every time a chunk is received. That is why you get multiple output messages...
Hopefully this way you can get some data from your server...
Works great!! Can't thank you enough.
I added a function to convert the payload to string "msg.payload = msg.payload.toString('utf8');"
The output is now just plain text. I Do you think there is a way to get this as an xml or something that's easier to work with or do I need to use regex or jonata?
Most probably others in this community will be able to give you a better answer.
But personally I wouldn't use xml, and convert it instead to json. Since json and jsonata is the standard in Node-RED, you will be able to use your data in lots of other nodes...
For example here you can find an example of the xml2js npm library.
Don't forget to add that also to your settings.js file: