# Http request (open connection)

**URL:** https://discourse.nodered.org/t/http-request-open-connection/32459
**Category:** General
**Created:** [4 September 2020 20:16 UTC](https://discourse.nodered.org/t/http-request-open-connection/32459 "2020-09-04T20:16:56Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![niclas.w82](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/niclas.w82/32/31813_2.png) [@niclas.w82](https://discourse.nodered.org/u/niclas.w82)
#### Post date: [4 September 2020 20:16 UTC](https://discourse.nodered.org/t/http-request-open-connection/32459/1 "2020-09-04T20:16:56Z")

</div>

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.

 ![Skärmavbild 2020-09-04 kl. 22.05.12](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/f/ffef5bc78f03e5eee94d9b61d35252561654b949.jpeg)

/Niclas

---

<div class="post-metadata">

### Author: ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)
#### Post date: [5 September 2020 04:57 UTC](https://discourse.nodered.org/t/http-request-open-connection/32459/2 "2020-09-05T04:57:27Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)
#### Post date: [5 September 2020 11:39 UTC](https://discourse.nodered.org/t/http-request-open-connection/32459/3 "2020-09-05T11:39:31Z")

</div>

> [@niclas.w82](#):
>
> I tried the multipart decoder and it succseeded to get the latest event and then the the node says (no multipart

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](https://flows.nodered.org/node/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

---

<div class="post-metadata">

### Author: ![niclas.w82](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/niclas.w82/32/31813_2.png) [@niclas.w82](https://discourse.nodered.org/u/niclas.w82)
#### Post date: [5 September 2020 19:29 UTC](https://discourse.nodered.org/t/http-request-open-connection/32459/4 "2020-09-05T19:29:39Z")

</div>

Hi Bart. I attach som images from the api documentation and a snippet from Wireshark.  
The request url is [http://192.168.80.121/arx/eventexport?end\_date=keep](http://192.168.80.121/arx/eventexport?end_date=keep).

Best regards Niclas

 ![Skärmavbild 2020-09-05 kl. 21.19.58](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/4/042c8a4797185c4a979bff0160a2a2aac7f2b738.png) ![Skärmavbild 2020-09-05 kl. 21.21.52](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/3/73442585808a6a3d378b26fde5bf5b69d4fae606.png) ![Skärmavbild 2020-09-05 kl. 21.22.16](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/9/f9ae69043825a421176a978d74cde28b62e6b926.png)

---

<div class="post-metadata">

### Author: ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)
#### Post date: [5 September 2020 20:50 UTC](https://discourse.nodered.org/t/http-request-open-connection/32459/5 "2020-09-05T20:50:04Z")

</div>

Niclas,  
Just guessing based on this:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/5/d5a16cc468d63c90ded79f44d9a31106f434f77c.png)

This is [chunked transfer encoding](https://en.wikipedia.org/wiki/Chunked_transfer_encoding) which is the default encoding. The http-request node will (see [code](https://github.com/node-red/node-red/blob/master/packages/node_modules/%40node-red/nodes/core/network/21-httprequest.js#L337)) 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](https://github.com/request/request) could perhaps be of any help to you. Suppose you rewrite it a bit like this (out of my head, so not tested!):

```auto
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](https://nodered.org/docs/user-guide/writing-functions#loading-additional-modules)). See also [this](https://discourse.nodered.org/t/importing-package/9900) discussion...

---

<div class="post-metadata">

### Author: ![niclas.w82](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/niclas.w82/32/31813_2.png) [@niclas.w82](https://discourse.nodered.org/u/niclas.w82)
#### Post date: [6 September 2020 14:10 UTC](https://discourse.nodered.org/t/http-request-open-connection/32459/6 "2020-09-06T14:10:04Z")

</div>

Thank you Bart. I think I need to spend some time reading now and try to understand how to add the library.

Many Thanks

---

<div class="post-metadata">

### Author: ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)
#### Post date: [6 September 2020 19:39 UTC](https://discourse.nodered.org/t/http-request-open-connection/32459/7 "2020-09-06T19:39:01Z")

</div>

> [@niclas.w82](#):
>
> try to understand how to add the library.

Niclas,  
Since I have no clue whether this will work, I will try to minimize the effort you spend to it...

1. You need to add one line to your settings.js file (and restart Node-RED):

2. Import this test flow:

3. Inside the function node I call the first http1.1 test [url](https://jigsaw.w3.org/HTTP/ChunkedScript) from this [page](https://jigsaw.w3.org/HTTP/):

4. When you trigger the function node, you will see now multiple entries in the debug sidebar:

Hopefully this way you can get some data from your server...

---

<div class="post-metadata">

### Author: ![niclas.w82](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/niclas.w82/32/31813_2.png) [@niclas.w82](https://discourse.nodered.org/u/niclas.w82)
#### Post date: [6 September 2020 20:56 UTC](https://discourse.nodered.org/t/http-request-open-connection/32459/8 "2020-09-06T20:56:29Z")

</div>

Hi Bart.

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?

 ![Skärmavbild 2020-09-06 kl. 22.39.21](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/b/0b756fc23b1dc5148125f5a57089f4b97094ab2c.png)

Best regard Niclas

---

<div class="post-metadata">

### Author: ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)
#### Post date: [6 September 2020 21:06 UTC](https://discourse.nodered.org/t/http-request-open-connection/32459/9 "2020-09-06T21:06:54Z")

</div>

> [@niclas.w82](#):
>
> 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](https://stackoverflow.com/a/47711499) you can find an example of the _ **xml2js** _ npm library.  
Don't forget to add that also to your settings.js file:

```auto
    functionGlobalContext: {
        // os:require('os'),
        // jfive:require("johnny-five"),
        // j5board:require("johnny-five").Board({repl:false})
        request:require('request'),
        xml2js:require('xml2js')
    },

```

And add this line at the top of your function node:

```auto
var xml2js = global.get('xml2js');

```

But there might be better or easier ways to do this, I assume ...

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [6 September 2020 21:10 UTC](https://discourse.nodered.org/t/http-request-open-connection/32459/10 "2020-09-06T21:10:37Z")

</div>

.... like using the xml node (that uses xml2js)

---

<div class="post-metadata">

### Author: ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)
#### Post date: [6 September 2020 21:10 UTC](https://discourse.nodered.org/t/http-request-open-connection/32459/11 "2020-09-06T21:10:45Z")

</div>

Or simply follow this: [https://cookbook.nodered.org/basic/convert-xml](https://cookbook.nodered.org/basic/convert-xml)

---

<div class="post-metadata">

### Author: ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)
#### Post date: [6 September 2020 21:11 UTC](https://discourse.nodered.org/t/http-request-open-connection/32459/12 "2020-09-06T21:11:45Z")

</div>

Damn, Dave was first 😉  
Don't add it to your function node, if there is already another node that does the job very well ...

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [20 September 2020 21:11 UTC](https://discourse.nodered.org/t/http-request-open-connection/32459/13 "2020-09-20T21:11:58Z")

</div>

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