HTTP Post template to receive JSON payload

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!

Flow:

[{"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":[]}]

What do you see when you press the inject?

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

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.

1 Like

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...

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

image

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

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)
Node-red gives the ability to load external libraries using the functionExternalModules (link)

image

Example Flow

[{"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

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

1 Like

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)...

I'll post the demo flow shortly

Here is a working flow

Use CTRL+I to import it.

[{"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":[]}]

3 Likes

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

1 Like

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