# HTTP Post template to receive JSON payload

**URL:** <https://discourse.nodered.org/t/http-post-template-to-receive-json-payload/55963>\
**Category:** General\
**Tags:** http-request\
**Created:** [1 January 2022 18:24 UTC](https://discourse.nodered.org/t/http-post-template-to-receive-json-payload/55963 "2022-01-01T18:24:11Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![thejollypop](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thejollypop/32/36597_2.png) [@thejollypop](https://discourse.nodered.org/u/thejollypop)\
**Post date:** [1 January 2022 18:24 UTC](https://discourse.nodered.org/t/http-post-template-to-receive-json-payload/55963/1 "2022-01-01T18:24:11Z")

</div>

Hi all, my flow parses textual data using a template and GET method, however I am unable to post the result programmatically as a JSON.  
I want it to be generic and simple, without using `scope.send` or the 'form' method - and still be able to receive the results as `msg.payload` with the `http request` node. What am I missing? Thanks!

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/c/8c5f7dd71ed104e4889b090ca2909a1d7bb7fe08.png)

Flow:

```auto
[{"id":"a55f8f0f93e84992","type":"tab","label":"POST","disabled":false,"info":"","env":[]},{"id":"66b62d27f7d3b699","type":"http in","z":"a55f8f0f93e84992","name":"","url":"/dwvparser1","method":"get","upload":false,"swaggerDoc":"","x":120,"y":140,"wires":[["f1320111617c92f0"]]},{"id":"f1320111617c92f0","type":"template","z":"a55f8f0f93e84992","name":"DWV Parser","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<script type=\"text/javascript\" src=\"https://github.com/ivmartel/dwv/releases/download/v0.30.4/dwv-0.30.4.min.js\"></script>\n\n<div id=\"tags\"></div>\n\n<script type=\"text/javascript\">\n\n var url = 'https://raw.githubusercontent.com/ivmartel/dwv/master/tests/data/bbmri-53323851.dcm';\n\n var onloadtest = function(event) {\n // setup the dicom parser\n var dicomParser = new dwv.dicom.DicomParser();\n // parse the buffer\n dicomParser.parse(event.target.response);\n\n // div to display text\n var div = document.getElementById('tags');\n\n // get the raw dicom tags\n var rawTags = dicomParser.getRawDicomElements();\n // display the modality\n div.appendChild(document.createTextNode(\n\n '|Modality: ' + rawTags.x00080060.value[0] +\n '|Study Description: ' + rawTags.x00081030.value[0] +\n '|Series Description: ' + rawTags.x0008103E.value[0] +\n '|Patient ID: ' + rawTags.x00100020.value[0] +\n '|Patient Name: ' + rawTags.x00100010.value[0] +\n '|Gender: ' + rawTags.x00100040.value[0]\n ));\n\n // break line\n div.appendChild(document.createElement('br'));\n\n // get the wrapped dicom tags\n var tags = dicomParser.getDicomElements();\n // display the modality\n div.appendChild(document.createTextNode(\n 'Modality (bis): ' + tags.getFromName('Modality')\n ));\n };\n\n var request = new XMLHttpRequest();\n request.open('GET', url);\n request.responseType = 'arraybuffer';\n request.onload = onloadtest;\n request.send();\n \n // scope.send({payload: request.send() }); \n\n</script>","output":"str","x":310,"y":140,"wires":[["fd496ef1564d0ccc"]]},{"id":"fd496ef1564d0ccc","type":"http response","z":"a55f8f0f93e84992","name":"","statusCode":"","headers":{},"x":470,"y":140,"wires":[]},{"id":"f5c3a58572e4bf4c","type":"comment","z":"a55f8f0f93e84992","name":"scope.send?","info":"","x":310,"y":100,"wires":[]},{"id":"dcc6e4cff18cc687","type":"http request","z":"a55f8f0f93e84992","name":"POST request","method":"POST","ret":"obj","paytoqs":"ignore","url":"http://localhost:1880/dwvparser1","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"credentials":{},"x":260,"y":260,"wires":[["ddb924d633c036e2"]]},{"id":"ddb924d633c036e2","type":"debug","z":"a55f8f0f93e84992","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":430,"y":260,"wires":[]},{"id":"8e589c2a4ea9ec1e","type":"inject","z":"a55f8f0f93e84992","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":110,"y":260,"wires":[["dcc6e4cff18cc687"]]},{"id":"4a95acbc48f11200","type":"comment","z":"a55f8f0f93e84992","name":"Send HTTP POST request","info":"","x":150,"y":200,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [1 January 2022 19:15 UTC](https://discourse.nodered.org/t/http-post-template-to-receive-json-payload/55963/2 "2022-01-01T19:15:09Z")

</div>

What do you see when you press the inject?

---

<div class="post-metadata">

**Author:** ![thejollypop](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thejollypop/32/36597_2.png) [@thejollypop](https://discourse.nodered.org/u/thejollypop)\
**Post date:** [1 January 2022 19:23 UTC](https://discourse.nodered.org/t/http-post-template-to-receive-json-payload/55963/3 "2022-01-01T19:23:41Z")

</div>

I see the template node raw code, instead of the expected result when I go to the "/dwvparser1" endpoint.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [1 January 2022 19:45 UTC](https://discourse.nodered.org/t/http-post-template-to-receive-json-payload/55963/4 "2022-01-01T19:45:47Z")

</div>

And what does the data look like that you are getting from the URL you specify?  
[EDIT] it looks like th data is value since I dropped it into a dicom dump but I'm going to bow out since I've never attempted what you are doing with the `document.createTextNode`.

Hopefully someone with experience using that will jump in.

---

<div class="post-metadata">

**Author:** ![thejollypop](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thejollypop/32/36597_2.png) [@thejollypop](https://discourse.nodered.org/u/thejollypop)\
**Post date:** [1 January 2022 20:22 UTC](https://discourse.nodered.org/t/http-post-template-to-receive-json-payload/55963/5 "2022-01-01T20:22:37Z")

</div>

Thanks:) the payload should eventually display the following text:

> |Modality: MR|Study Description: INVESTIGATORS^Borjan|Series Description: MEMPRAGE\_4e\_p2\_1mm\_iso RMS|Patient ID: 12.02.15-15:49:23-STD-1.3.12.2.1107.5.2.32.35162|Patient Name: 20120215\_ASLtest\_human|Gender: M  
> Modality (bis): MR

I believe it can be done with `scope.send`, but I was wondering if there is a simpler and more generic solution to post the result...

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [1 January 2022 21:13 UTC](https://discourse.nodered.org/t/http-post-template-to-receive-json-payload/55963/6 "2022-01-01T21:13:29Z")

</div>

It looks like you did not export all of the `http in` nodes -- here is the error message I see:

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

The `http request` node is trying to POST to `http://localhost:1880/dwvparser1` -- is that endpoint supposed to be another node-red flow endpoint?

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [1 January 2022 21:30 UTC](https://discourse.nodered.org/t/http-post-template-to-receive-json-payload/55963/7 "2022-01-01T21:30:58Z")

</div>

so your end goal is to get the data back and not to create a new url endpoint `/dwvparser1` ?  
in that case do an http request directly to your _dcm_ url

I experimented a bit with an npm dicomParser library ([link](https://github.com/cornerstonejs/dicomParser))  
Node-red gives the ability to load external libraries using the `functionExternalModules` ([link](https://nodered.org/docs/user-guide/writing-functions#using-the-functionexternalmodules-option))

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/0/2064a3fd646dfc894c9280f645582d08cb9bc23e.png)

**Example Flow**

```auto
[{"id":"32f6f4572695fd78","type":"inject","z":"a55f8f0f93e84992","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":190,"y":480,"wires":[["93e94d64aad567c0"]]},{"id":"93e94d64aad567c0","type":"http request","z":"a55f8f0f93e84992","name":"","method":"GET","ret":"bin","paytoqs":"ignore","url":"https://raw.githubusercontent.com/ivmartel/dwv/master/tests/data/bbmri-53323851.dcm","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"x":380,"y":480,"wires":[["df3e2bc3794bead3"]]},{"id":"314b2564eeb18860","type":"debug","z":"a55f8f0f93e84992","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":710,"y":480,"wires":[]},{"id":"df3e2bc3794bead3","type":"function","z":"a55f8f0f93e84992","name":"","func":"let byteArray = msg.payload\n\ntry {\n // Parse the byte array to get a DataSet object that has the parsed contents\n var dataSet = dicomParser.parseDicom(byteArray/*, options */);\n node.send({dataSet})\n\n // access a string element\n var studyInstanceUid = dataSet.string('x00080023');\n node.send({ studyInstanceUid });\n\n // get the pixel data element (contains the offset and length of the data)\n var pixelDataElement = dataSet.elements.x7fe00010;\n node.send({ pixelDataElement });\n // create a typed array on the pixel data (this example assumes 16 bit unsigned data)\n // var pixelData = new Uint16Array(dataSet.byteArray.buffer, pixelDataElement.dataOffset, pixelDataElement.length / 2);\n}\ncatch (ex) {\n node.warn('Error parsing byte stream');\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"dicomParser","module":"dicom-parser"}],"x":540,"y":480,"wires":[["314b2564eeb18860"]]}]

```

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/4/b401de988fa5be52132ad74546129d91b3bf7d01.png)

PS .. hope you find some of it useful .. end of the road for me as i have no experience to help further.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [1 January 2022 21:44 UTC](https://discourse.nodered.org/t/http-post-template-to-receive-json-payload/55963/8 "2022-01-01T21:44:29Z")

</div>

Hi i think you are confused with how things like this works.

The inject you have calls the HTTP Endpoint then your template is returned. In your template I can see you have some html fragment and some scripts. These would normally be executed in a browser.

The HTTP Request simply goes off to a remote endpoint (in your case the endpoint `dwvparser1`) and GETs the data - in your case, a HTML Fragment and script tags - the HTTP Request node is not a browser - these scripts will never execute.

I suspect what you actually want is a node js DICOM parser (without the whole endpoint stuff)

Here is an example of parsing a DCM file without a browser (i.e. completely inside the node-red server side runtime)...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/2/a2a0b4b8c5a3e6cc307ee9dd024a20157b91d772.png)

I'll post the demo flow shortly

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [1 January 2022 21:50 UTC](https://discourse.nodered.org/t/http-post-template-to-receive-json-payload/55963/9 "2022-01-01T21:50:51Z")

</div>

Here is a working flow

Use CTRL+I to import it.

```auto
[{"id":"089792404c9ee4b8","type":"function","z":"a55f8f0f93e84992","name":"get patient summary","func":"const dicomFileAsBuffer = msg.payload;\n\ntry {\n const dataSet = dicomParser.parseDicom(dicomFileAsBuffer);\n const patient = {\n modality: dataSet.string('x00080060'),\n studyDescription: dataSet.string('x00081030'),\n seriesDescription: dataSet.string('x0008103E'),\n patientID: dataSet.string('x00100020'),\n gender: dataSet.string('x00100040'),\n patientName: dataSet.string('x00100010'),\n }\n msg.payload = patient;\n return msg;\n} catch (ex) {\n node.error(ex, msg);\n}\n\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"dicomParser","module":"dicom-parser"}],"x":1820,"y":180,"wires":[["7afc5bd18b143ef5"]]},{"id":"23968a7add27a810","type":"inject","z":"a55f8f0f93e84992","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1760,"y":120,"wires":[["80a182546358da7d"]]},{"id":"7afc5bd18b143ef5","type":"debug","z":"a55f8f0f93e84992","name":"summary","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":2020,"y":180,"wires":[]},{"id":"80a182546358da7d","type":"http request","z":"a55f8f0f93e84992","name":"http get bbmri-53323851.dcm","method":"GET","ret":"bin","paytoqs":"ignore","url":"https://raw.githubusercontent.com/ivmartel/dwv/master/tests/data/bbmri-53323851.dcm","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"x":1980,"y":120,"wires":[["089792404c9ee4b8"]]},{"id":"a1cc17ca8be86649","type":"inject","z":"a55f8f0f93e84992","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1760,"y":260,"wires":[["3637bb4fca24b108"]]},{"id":"3637bb4fca24b108","type":"http request","z":"a55f8f0f93e84992","name":"http get bbmri-53323851.dcm","method":"GET","ret":"bin","paytoqs":"ignore","url":"https://raw.githubusercontent.com/ivmartel/dwv/master/tests/data/bbmri-53323851.dcm","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"x":1980,"y":260,"wires":[["d8033b34f47d86dc"]]},{"id":"d8033b34f47d86dc","type":"function","z":"a55f8f0f93e84992","name":"get all values","func":"const dicomFileAsBuffer = msg.payload;\ntry {\n const dataSet = dicomParser.parseDicom(dicomFileAsBuffer);\n const data = {};\n Object.values(dataSet.elements).forEach(e => { \n data[e.tag] = dataSet.string(e.tag);\n }) \n msg.payload = data;\n return msg;\n} catch (ex) {\n node.error(ex, msg);\n}\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"dicomParser","module":"dicom-parser"}],"x":1790,"y":320,"wires":[["482e04d16227f0f6"]]},{"id":"482e04d16227f0f6","type":"debug","z":"a55f8f0f93e84992","name":"all values","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":2020,"y":320,"wires":[]}]

```

[![](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/2/a2a0b4b8c5a3e6cc307ee9dd024a20157b91d772.png) ](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/2/a2a0b4b8c5a3e6cc307ee9dd024a20157b91d772.png)

---

<div class="post-metadata">

**Author:** ![thejollypop](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thejollypop/32/36597_2.png) [@thejollypop](https://discourse.nodered.org/u/thejollypop)\
**Post date:** [1 January 2022 21:57 UTC](https://discourse.nodered.org/t/http-post-template-to-receive-json-payload/55963/10 "2022-01-01T21:57:42Z")

</div>

@UnborN @Steve-Mcl I appreciate your help. It's working!

---

<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:** [15 January 2022 21:58 UTC](https://discourse.nodered.org/t/http-post-template-to-receive-json-payload/55963/11 "2022-01-15T21:58:23Z")

</div>

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