Msg.payload with path and filename to file out node

Dear community,

i get this palyoad string from a node:

"{"path":"/config/www/images","filename":"cam1_20210329-210317.jpg","dev":2056,"mode":33188,"nlink":1,"uid":0,"gid":0,"rdev":0,"blksize":4096,"ino":142277,"size":12112,"blocks":24,"atimeMs":1617047253948.755,"mtimeMs":1617047238237.5022,"ctimeMs":1617047238237.5022,"birthtimeMs":1617047238235.5022,"atime":"2021-03-29T19:47:33.949Z","mtime":"2021-03-29T19:47:18.238Z","ctime":"2021-03-29T19:47:18.238Z","birthtime":"2021-03-29T19:47:18.236Z"}"

i only want to put the "path" and "filename", while the filename is variable, to create a new file with a file node. This was my try: change msg.paload to msg.filename=path+filename, with no luck: file node give error: msg : string[21] "No filename specified".

Any help is appreciated.

Hello Sven,

So the msg.payload is a string ? It looks like a valid Json ..
In order to convert it to Javascript object you need to pass it from a Json node.
After that you'll be able to access its properties.

Example:

[{"id":"364d0c13.eae7a4","type":"inject","z":"6d1f3e0d.f0c68","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"path\":\"/config/www/images\",\"filename\":\"cam1_20210329-210317.jpg\",\"dev\":2056,\"mode\":33188,\"nlink\":1,\"uid\":0,\"gid\":0,\"rdev\":0,\"blksize\":4096,\"ino\":142277,\"size\":12112,\"blocks\":24,\"atimeMs\":1617047253948.755,\"mtimeMs\":1617047238237.5022,\"ctimeMs\":1617047238237.5022,\"birthtimeMs\":1617047238235.5022,\"atime\":\"2021-03-29T19:47:33.949Z\",\"mtime\":\"2021-03-29T19:47:18.238Z\",\"ctime\":\"2021-03-29T19:47:18.238Z\",\"birthtime\":\"2021-03-29T19:47:18.236Z\"}","payloadType":"str","x":240,"y":500,"wires":[["211e71b7.a98ea6"]]},{"id":"211e71b7.a98ea6","type":"json","z":"6d1f3e0d.f0c68","name":"","property":"payload","action":"","pretty":false,"x":400,"y":500,"wires":[["a78b63de.7c30f8","dabd2680.42c9a"]]},{"id":"a78b63de.7c30f8","type":"debug","z":"6d1f3e0d.f0c68","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":490,"y":440,"wires":[]},{"id":"dabd2680.42c9a","type":"function","z":"6d1f3e0d.f0c68","name":"","func":"msg.filename = msg.payload.path + \"/\" + msg.payload.filename;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":640,"y":500,"wires":[["a0880dd9.36ac98","12131a94.40fcf5"]]},{"id":"a0880dd9.36ac98","type":"debug","z":"6d1f3e0d.f0c68","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":750,"y":440,"wires":[]},{"id":"12131a94.40fcf5","type":"debug","z":"6d1f3e0d.f0c68","name":"3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"filename","targetType":"msg","statusVal":"","statusType":"auto","x":750,"y":560,"wires":[]}]

Sven, welcome to the forum!

Not seeing your flow it is hard to know if that is a string on not. If you send it to a debug node and it shows as a string, run it thru a json node and then to a debug node to see what the object looks like.

You can then click the >_ icon to copy the path to each element and then try creating the file name.

Does this mystery node have an option to output a JS object?

Also when you do get the output as an object msg.filename=path+filename the path and filename should start msg., unless you created the vars beforehand.

Hi, thanks for the reply´s,

@ UnborN your example is working, however, after i integreated it into my flow
the result is: msg.filename : string[19] "undefined/undefined".

I will post the complete flow.

I also don´t want to put the path and filename into a now file but only the filename into a
curl POST as a variable for the jpg file in the exec node: CurlPostRocketChat in:
"attachments":[{"image_url":"http://192.168.0.8:8123/local/images/cam1_20210329-210317.jpg"}]}' is that even possible?

Thanks in advance for any help.

My flow

[{"id":"f9d7b0ee.037d2","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"6456763b.e07d78","type":"fs-ops-dir","z":"f9d7b0ee.037d2","name":"","path":"payload","pathType":"msg","filter":"*.jpg","filterType":"str","dir":"files","dirType":"msg","x":260,"y":60,"wires":[["35811671.90ebea"]]},{"id":"35811671.90ebea","type":"fs-ops-stats","z":"f9d7b0ee.037d2","name":"","path":"payload","pathType":"msg","filename":"files","filenameType":"msg","stats":"stats","sizeType":"msg","x":440,"y":60,"wires":[["ee6515ea.f968c8"]]},{"id":"7dbdf6ef.a73868","type":"inject","z":"f9d7b0ee.037d2","name":"RootDir","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"/config/www/images","payloadType":"str","x":90,"y":60,"wires":[["6456763b.e07d78"]]},{"id":"ee6515ea.f968c8","type":"function","z":"f9d7b0ee.037d2","name":"Combine and sort","func":"var combined = [];\n\nfor (var i = 0; i < msg.files.length; i++) {\n    var file = msg.files[i];\n    var stat = msg.stats[i];\n    \n    combined.push({\n        path: msg.payload,\n        file,\n             ...stat\n    })\n}\n\ncombined.sort((a, b) => b.birthtimeMs - a.birthtimeMs);\n\nmsg.combined = combined;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":630,"y":60,"wires":[["50034043.e2347"]]},{"id":"50034043.e2347","type":"change","z":"f9d7b0ee.037d2","name":"payload combined","rules":[{"t":"set","p":"payload","pt":"msg","to":"combined[0]","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":130,"y":140,"wires":[["a3db201d.8b0cd"]]},{"id":"da5b725.4352b9","type":"change","z":"f9d7b0ee.037d2","name":"file to filename","rules":[{"t":"change","p":"payload","pt":"msg","from":"file","fromt":"str","to":"filename","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":140,"wires":[["ac191147.385c7"]]},{"id":"a3db201d.8b0cd","type":"json","z":"f9d7b0ee.037d2","name":"","property":"payload","action":"","pretty":false,"x":310,"y":140,"wires":[["da5b725.4352b9"]]},{"id":"5c0992f4.93919c","type":"debug","z":"f9d7b0ee.037d2","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":510,"y":220,"wires":[]},{"id":"d2d5deb5.84213","type":"debug","z":"f9d7b0ee.037d2","name":"3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"filename","targetType":"msg","statusVal":"","statusType":"auto","x":670,"y":280,"wires":[]},{"id":"ac191147.385c7","type":"function","z":"f9d7b0ee.037d2","name":"","func":"msg.filename = msg.payload.path + \"/\" + msg.payload.filename;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":660,"y":140,"wires":[["5c0992f4.93919c","d2d5deb5.84213"]]},{"id":"d78ea1d9.30681","type":"exec","z":"f9d7b0ee.037d2","command":"curl -X POST -H \"Content-type:application/json\" -d '{\"text\":\"Es hat gerade geklingelt!\",\"attachments\":[{\"image_url\":\"http://192.168.0.8:8123/local/images/cam1_20210329-210317.jpg\"}]}' http://192.168.147.155:3000/hooks/EBKNzvBtZbBtCCyRG/hTyvpugcp85dXnyuogm8XxD8GXH9bvEDimvdNZEyw3WCTibQ","addpay":false,"append":"","useSpawn":"false","timer":"5","oldrc":false,"name":"CurlPostRocketChat","x":460,"y":320,"wires":[[],[],[]]}]

I wasnt familiar with those fs-ops nodes but I think the Json node wasnt needed after all
You original post was a little bit misleading because you said the payload was a string :wink:

So you are reading a folder and getting the most recent jpg file name
and you need to add that filename to a curl command ?

The exec node that you were using has an option to append a msg.payload
So in the preceding function prepare the parameters to append to the curl command

image

[{"id":"7dbdf6ef.a73868","type":"inject","z":"f9d7b0ee.037d2","name":"RootDir","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"/config/www/images","payloadType":"str","x":110,"y":100,"wires":[["6456763b.e07d78"]]},{"id":"ee6515ea.f968c8","type":"function","z":"f9d7b0ee.037d2","name":"Combine and sort","func":"var combined = [];\n\nfor (var i = 0; i < msg.files.length; i++) {\n    var file = msg.files[i];\n    var stat = msg.stats[i];\n    \n    combined.push({\n        path: msg.payload,\n        file,\n             ...stat\n    })\n}\n\ncombined.sort((a, b) => b.birthtimeMs - a.birthtimeMs);\n\nmsg.combined = combined;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":630,"y":160,"wires":[["50034043.e2347","493c6290.125e14"]]},{"id":"50034043.e2347","type":"change","z":"f9d7b0ee.037d2","name":"payload combined","rules":[{"t":"set","p":"payload","pt":"msg","to":"combined[0]","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":870,"y":160,"wires":[["5c0992f4.93919c","ac191147.385c7"]]},{"id":"5c0992f4.93919c","type":"debug","z":"f9d7b0ee.037d2","name":"3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1010,"y":100,"wires":[]},{"id":"d2d5deb5.84213","type":"debug","z":"f9d7b0ee.037d2","name":"4","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1230,"y":100,"wires":[]},{"id":"ac191147.385c7","type":"function","z":"f9d7b0ee.037d2","name":"","func":"\n// prepare curl parameters to append with msg.payload\n\nlet cmd = `-X POST -H \"Content-type:application/json\" -d '{\"text\":\"Es hat gerade geklingelt!\",\"attachments\":[{\"image_url\":\"http://192.168.0.8:8123/local/images/${msg.payload.file}\"}]}' http://192.168.147.155:3000/hooks/EBKNzvBtZbBtCCyRG/hTyvpugcp85dXnyuogm8XxD8GXH9bvEDimvdNZEyw3WCTibQ`\n\nmsg.payload = cmd\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1100,"y":160,"wires":[["d2d5deb5.84213","d78ea1d9.30681"]]},{"id":"d78ea1d9.30681","type":"exec","z":"f9d7b0ee.037d2","command":"curl","addpay":true,"append":"","useSpawn":"false","timer":"5","oldrc":false,"name":"CurlPostRocketChat","x":660,"y":340,"wires":[["bfe4d4bb.73804"],[],[]]},{"id":"35811671.90ebea","type":"fs-ops-stats","z":"f9d7b0ee.037d2","name":"","path":"payload","pathType":"msg","filename":"files","filenameType":"msg","stats":"stats","sizeType":"msg","x":400,"y":160,"wires":[["ee6515ea.f968c8","e85f98ed.d60d58"]]},{"id":"6456763b.e07d78","type":"fs-ops-dir","z":"f9d7b0ee.037d2","name":"","path":"payload","pathType":"msg","filter":"*.jpg","filterType":"str","dir":"files","dirType":"msg","x":240,"y":160,"wires":[["35811671.90ebea"]]},{"id":"e85f98ed.d60d58","type":"debug","z":"f9d7b0ee.037d2","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":510,"y":100,"wires":[]},{"id":"493c6290.125e14","type":"debug","z":"f9d7b0ee.037d2","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":790,"y":100,"wires":[]},{"id":"bfe4d4bb.73804","type":"debug","z":"f9d7b0ee.037d2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":880,"y":320,"wires":[]}]

@UnborN,

you are a star, thank you so much! I spent so many hours on that
and couldn´t make it work. Now it´s working like a charm. :grinning:

Hi @svengem is there a particular reason you are spawning out to curl instead of using the built in http request node?

In case you want to try it - this should work (untested as I dont have your end points or files)

[{"id":"7dbdf6ef.a73868","type":"inject","z":"df3cdacd.dc9198","name":"RootDir","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"/config/www/images","payloadType":"str","x":470,"y":680,"wires":[["6456763b.e07d78"]]},{"id":"ee6515ea.f968c8","type":"function","z":"df3cdacd.dc9198","name":"Combine and sort","func":"var combined = [];\n\nfor (var i = 0; i < msg.files.length; i++) {\n    var file = msg.files[i];\n    var stat = msg.stats[i];\n    \n    combined.push({\n        path: msg.payload,\n        file,\n             ...stat\n    })\n}\n\ncombined.sort((a, b) => b.birthtimeMs - a.birthtimeMs);\n\nmsg.combined = combined;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":990,"y":740,"wires":[["50034043.e2347","493c6290.125e14"]]},{"id":"50034043.e2347","type":"change","z":"df3cdacd.dc9198","name":"payload combined","rules":[{"t":"set","p":"payload","pt":"msg","to":"combined[0]","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1230,"y":740,"wires":[["5c0992f4.93919c","fff67376.d467a2"]]},{"id":"5c0992f4.93919c","type":"debug","z":"df3cdacd.dc9198","name":"3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1370,"y":680,"wires":[]},{"id":"d2d5deb5.84213","type":"debug","z":"df3cdacd.dc9198","name":"4","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1590,"y":680,"wires":[]},{"id":"35811671.90ebea","type":"fs-ops-stats","z":"df3cdacd.dc9198","name":"","path":"payload","pathType":"msg","filename":"files","filenameType":"msg","stats":"stats","sizeType":"msg","x":760,"y":740,"wires":[["ee6515ea.f968c8","e85f98ed.d60d58"]]},{"id":"6456763b.e07d78","type":"fs-ops-dir","z":"df3cdacd.dc9198","name":"","path":"payload","pathType":"msg","filter":"*.jpg","filterType":"str","dir":"files","dirType":"msg","x":600,"y":740,"wires":[["35811671.90ebea"]]},{"id":"e85f98ed.d60d58","type":"debug","z":"df3cdacd.dc9198","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":870,"y":680,"wires":[]},{"id":"493c6290.125e14","type":"debug","z":"df3cdacd.dc9198","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1150,"y":680,"wires":[]},{"id":"fff67376.d467a2","type":"function","z":"df3cdacd.dc9198","name":"Prepare request","func":"//original curl:  curl -X POST -H \"Content-type:application/json\" -d '{\"text\":\"Es hat gerade geklingelt!\",\"attachments\":[{\"image_url\":\"http://192.168.0.8:8123/local/images/FILENAME\"}]}' http://192.168.147.155:3000/hooks/EBKNzvBtZbBtCCyRG/hTyvpugcp85dXnyuogm8XxD8GXH9bvEDimvdNZEyw3WCTibQ\nmsg.url = \"http://192.168.147.155:3000/hooks/EBKNzvBtZbBtCCyRG/hTyvpugcp85dXnyuogm8XxD8GXH9bvEDimvdNZEyw3WCTibQ\";\nmsg.payload = { \n    \"text\": \"Es hat gerade geklingelt!\", \n    \"attachments\": [\n        { \n            \"image_url\": `http://192.168.0.8:8123/local/images/${msg.payload.file}` \n        }\n    ] \n};\nmsg.headers = { \"Content-type\": \"application/json\" };\nmsg.cookies = null;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1480,"y":740,"wires":[["fff67376.d467a3","d2d5deb5.84213"]]},{"id":"fff67376.d467a3","type":"http request","z":"df3cdacd.dc9198","name":"","method":"use","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","x":1390,"y":820,"wires":[["fff67376.d467a4"]]},{"id":"fff67376.d467a4","type":"debug","z":"df3cdacd.dc9198","name":"curl result","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1600,"y":820,"wires":[]}]
1 Like

its much cleaner with Steve's Http request node example :+1:

i noticed you have it set to :
image

in your preceding function you will also need msg.method = "POST";
or to set the Method directly to POST

1 Like

Yeah I missed that. I'm working on a curl to node-red convertor and this was missed from my code. Cheers.

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