[RESOLVE] FR/EN - CSV line by line / API / Write CSV

Hello,
I'm new to Node Red, and I can't solve a problem.
My objective, to read a CSV file, line by line.
For each line execute an action towards a server, wait for the response, write this response then go to the next line.
My basic CSV is composed as follows:
Name ;IP_address
AAA ;192.168.1.10
BBB;192.168.1.11
….
So I want to read line 2, to call my API, wait for the return, and write the response in a new CSV in the form:
Name ;IP_Address ;API_Response

Below is what I did. In fact I can't get it to read line by line and then wait for the response.

Thanks for your help !

Fr
Bonjour,

Je suis nouveau utilisateur sur Node Red, et je n’arrive pas à résoudre un problème.

Mon objectif, lire un fichier CSV, ligne par ligne.

Pour chaque ligne exécuter une action vers un serveur, attendre la réponse, écrire cette réponse puis passer à la ligne suivante.

Mon CSV de base est composé ainsi :

Name ;Adresse_IP

AAA ;192.168.1.10

BBB ;192.168.1.11

….

Donc je souhaite lire la ligne n°2, vers appel à mon API, attendre le retour, et inscrire dans un nouveau CSV la réponse sous la forme :

Name ;Adresse_IP ;Réponse_API

Ci-dessous ce que j’ai fait. En fait je n’arrive pas à faire lire ligne par ligne puis attendre la réponse.

Merci pour votre aide !

[{"id":"5825ca337611592f","type":"tab","label":"Flow 5","disabled":false,"info":""},{"id":"d42eaa86.c12c18","type":"csv","z":"5825ca337611592f","name":"","sep":";","hdrin":true,"hdrout":"none","multi":"mult","ret":"\\r\\n","temp":"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":false,"x":750,"y":100,"wires":[["3ac5b1c2.cbbd5e"]]},{"id":"3ac5b1c2.cbbd5e","type":"split","z":"5825ca337611592f","name":"","splt":";","spltType":"str","arraySplt":"1","arraySpltType":"len","stream":true,"addname":"topic","x":890,"y":100,"wires":[["edaa2927b2ee2939"]]},{"id":"4e4829e3.eaeae8","type":"file in","z":"5825ca337611592f","name":"","filename":"C:/Users/augus/Desktop/testNodeR.csv","format":"utf8","chunk":false,"sendError":false,"encoding":"utf8","allProps":false,"x":460,"y":100,"wires":[["d42eaa86.c12c18"]]},{"id":"9238408f544a36cc","type":"xml","z":"5825ca337611592f","name":"","property":"payload","attr":"","chr":"name","x":630,"y":240,"wires":[["a0bd226f4571fc70"]]},{"id":"a0bd226f4571fc70","type":"jsonpath","z":"5825ca337611592f","expression":"$..Name","split":true,"name":"","x":810,"y":240,"wires":[["90b1d03a98efdb93"]]},{"id":"cb22e1c69458ea8d","type":"inject","z":"5825ca337611592f","name":"READ VALUE","repeat":"","crontab":"","once":false,"onceDelay":"","topic":"","payload":"","payloadType":"date","x":150,"y":100,"wires":[["4e4829e3.eaeae8"]]},{"id":"edaa2927b2ee2939","type":"function","z":"5825ca337611592f","name":"","func":"msg.url = msg.payload.AdIP;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":240,"y":240,"wires":[["20b938a61ed0b7bf"]]},{"id":"20b938a61ed0b7bf","type":"http request","z":"5825ca337611592f","name":"HTTP POST","method":"GET","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"basic","x":440,"y":240,"wires":[["9238408f544a36cc"]]},{"id":"90b1d03a98efdb93","type":"debug","z":"5825ca337611592f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1030,"y":240,"wires":[]}]

Will need a response message from server to help create new csv , but here is how to send the requests.
removed nodes that i do not have installed . Added a template to simulate the file read. as others will not have your csv file

[{"id":"cb22e1c69458ea8d","type":"inject","z":"5825ca337611592f","name":"READ VALUE","repeat":"","crontab":"","once":false,"onceDelay":"","topic":"","payload":"","payloadType":"date","x":150,"y":100,"wires":[["fd880fa8.6777e8"]]},{"id":"fd880fa8.6777e8","type":"template","z":"5825ca337611592f","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"Name ;Adresse_IP\nAAA ;192.168.1.10\nBBB ;192.168.1.11\n","output":"str","x":420,"y":160,"wires":[["d42eaa86.c12c18"]]},{"id":"d42eaa86.c12c18","type":"csv","z":"5825ca337611592f","name":"","sep":";","hdrin":true,"hdrout":"none","multi":"one","ret":"\\r\\n","temp":"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":false,"x":750,"y":100,"wires":[["edaa2927b2ee2939"]]},{"id":"4e4829e3.eaeae8","type":"file in","z":"5825ca337611592f","name":"","filename":"C:/Users/augus/Desktop/testNodeR.csv","format":"utf8","chunk":false,"sendError":false,"encoding":"utf8","x":460,"y":100,"wires":[["d42eaa86.c12c18"]]},{"id":"edaa2927b2ee2939","type":"function","z":"5825ca337611592f","name":"","func":"msg.url = msg.payload.Adresse_IP;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":240,"y":240,"wires":[["c6ead0f8.aa0a4","20b938a61ed0b7bf"]]},{"id":"c6ead0f8.aa0a4","type":"debug","z":"5825ca337611592f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":820,"y":180,"wires":[]},{"id":"20b938a61ed0b7bf","type":"http request","z":"5825ca337611592f","name":"HTTP POST","method":"GET","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"basic","x":440,"y":240,"wires":[["9238408f544a36cc"]]},{"id":"9238408f544a36cc","type":"xml","z":"5825ca337611592f","name":"","property":"payload","attr":"","chr":"name","x":630,"y":240,"wires":[["90b1d03a98efdb93"]]},{"id":"90b1d03a98efdb93","type":"debug","z":"5825ca337611592f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":830,"y":240,"wires":[]}]

Show us the resoponse xml if you need more help.

Thank you for the answer.
So I manage to send my requests well, and the server answers me well, except that node red sends all the requests at the same time, and I receive the answers according to the response time of the servers and not in order. So I don't know which servers are answering me...

that's why I want to send request1 -> wait for a response -> write the response then request 2-> ....

I hope to be understandable.
Thanks

Merci pour la réponse.
Alors j'arrive bien à envoyer mes requetes, et le serveur me répond bien, sauf que node red envoie toutes les requetes en même temps, et je recois les réponses en fonction du temps de réponse des serveurs et non pas dans l'ordre. Donc je ne sais pas quel serveurs me réponds...

c'est pour cela que je souhaites envoyer requete1 -> attendre une réponse -> ecrire la réponse puis requete 2-> ....

J'espere être compréhensible.
Merci

There should be no need to wait, use a join node to save the results. set parts.type to "object" and parts.key to the name in the function
e.g

[{"id":"cb22e1c69458ea8d","type":"inject","z":"5825ca337611592f","name":"READ VALUE","repeat":"","crontab":"","once":false,"onceDelay":"","topic":"","payload":"","payloadType":"date","x":150,"y":100,"wires":[["fd880fa8.6777e8"]]},{"id":"fd880fa8.6777e8","type":"template","z":"5825ca337611592f","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"Name ;Adresse_IP\nAAA ;192.168.1.10\nBBB ;192.168.1.11\n","output":"str","x":390,"y":100,"wires":[["d42eaa86.c12c18"]]},{"id":"d42eaa86.c12c18","type":"csv","z":"5825ca337611592f","name":"","sep":";","hdrin":true,"hdrout":"none","multi":"one","ret":"\\r\\n","temp":"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":false,"x":550,"y":100,"wires":[["edaa2927b2ee2939"]]},{"id":"edaa2927b2ee2939","type":"function","z":"5825ca337611592f","name":"","func":"msg.url = msg.payload.Adresse_IP;\nmsg.parts.key = msg.payload.Name;\nmsg.parts.type = \"object\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":240,"y":240,"wires":[["c6ead0f8.aa0a4","20b938a61ed0b7bf"]]},{"id":"c6ead0f8.aa0a4","type":"debug","z":"5825ca337611592f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":820,"y":180,"wires":[]},{"id":"20b938a61ed0b7bf","type":"http request","z":"5825ca337611592f","name":"HTTP POST","method":"GET","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"basic","x":440,"y":240,"wires":[["9238408f544a36cc"]]},{"id":"9238408f544a36cc","type":"xml","z":"5825ca337611592f","name":"","property":"payload","attr":"","chr":"name","x":630,"y":240,"wires":[["2e6691b3.6855a6"]]},{"id":"2e6691b3.6855a6","type":"join","z":"5825ca337611592f","name":"","mode":"auto","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":"false","timeout":"","count":"","reduceRight":false,"x":650,"y":300,"wires":[["90b1d03a98efdb93"]]},{"id":"90b1d03a98efdb93","type":"debug","z":"5825ca337611592f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":770,"y":240,"wires":[]}]

Then all results are stored in an object with there names as keys. You can then use the object to create your new csv file.

E1cid! Thank you very much for your help ! I managed to get my result!!!
I did a little differently but the result is present!

Thanks

E1cid ! Merci beaucoup pour ton aide ! j'ai réussi à arriver à mon résultat !!!
J'ai fait un peu autrement mais le résultat est présent !

Merci

Final :slight_smile:

[{"id":"5825ca337611592f","type":"tab","label":"Distech_API","disabled":false,"info":""},{"id":"d42eaa86.c12c18","type":"csv","z":"5825ca337611592f","name":"","sep":";","hdrin":true,"hdrout":"none","multi":"mult","ret":"\\r\\n","temp":"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":false,"x":790,"y":100,"wires":[["6376df751668ad1c"]]},{"id":"4e4829e3.eaeae8","type":"file in","z":"5825ca337611592f","name":"","filename":"C:/Users/augus/Desktop/UltimeNodeRed.csv","format":"utf8","chunk":false,"sendError":false,"encoding":"utf8","allProps":false,"x":490,"y":100,"wires":[[]]},{"id":"9238408f544a36cc","type":"xml","z":"5825ca337611592f","name":"","property":"payload","attr":"","chr":"name","x":930,"y":220,"wires":[["a0bd226f4571fc70","5edd11cabb2fdf1b"]]},{"id":"a0bd226f4571fc70","type":"jsonpath","z":"5825ca337611592f","expression":"$..Name","split":true,"name":"","x":1090,"y":220,"wires":[["c57270f4d7081711"]]},{"id":"cb22e1c69458ea8d","type":"inject","z":"5825ca337611592f","name":"Lecture Nom GFX","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"","topic":"","payloadType":"date","x":170,"y":100,"wires":[["22abcb215b7b327c"]]},{"id":"20b938a61ed0b7bf","type":"http request","z":"5825ca337611592f","name":"HTTP POST","method":"GET","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"basic","x":740,"y":220,"wires":[["9238408f544a36cc"]]},{"id":"6376df751668ad1c","type":"split","z":"5825ca337611592f","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":1000,"y":100,"wires":[["4b793f6d4416afcc"]]},{"id":"5edd11cabb2fdf1b","type":"function","z":"5825ca337611592f","name":"","func":"msg.payload=msg.url;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":600,"y":400,"wires":[["c57270f4d7081711"]]},{"id":"c57270f4d7081711","type":"join","z":"5825ca337611592f","name":"","mode":"custom","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":";","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":790,"y":400,"wires":[["07c7eb8cb541eef3","d04e4db223dfbf97"]]},{"id":"07c7eb8cb541eef3","type":"debug","z":"5825ca337611592f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1050,"y":400,"wires":[]},{"id":"d04e4db223dfbf97","type":"file","z":"5825ca337611592f","name":"","filename":"C:/Users/augus/Desktop/reponseNodeRed_GFX.csv","appendNewline":true,"createDir":false,"overwriteFile":"false","encoding":"none","x":1080,"y":460,"wires":[[]]},{"id":"86b67771cdfba8ca","type":"function","z":"5825ca337611592f","name":"Creation Adresse API Date/Time","func":"msg.url = \"http://\"+msg.payload.AdIP+\":8080/api/rest/v1/system/date-time\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":490,"y":740,"wires":[["12eb3e1fff0373c1"]]},{"id":"4b793f6d4416afcc","type":"function","z":"5825ca337611592f","name":"Creation Adresse API GFX","func":"msg.url = \"http://\"+msg.payload.AdIP+\":8080/api/rest/v1/files/bacnet/configuration/programs/program1.xml?encode=text\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":220,"wires":[["20b938a61ed0b7bf"]]},{"id":"26b31a39a94378ba","type":"inject","z":"5825ca337611592f","name":"Lecture Date/Heure","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"","topic":"","payloadType":"date","x":150,"y":660,"wires":[["667e5edf721dce43"]]},{"id":"b776025c13e24887","type":"file in","z":"5825ca337611592f","name":"","filename":"C:/Users/augus/Desktop/UltimeNodeRed.csv","format":"utf8","chunk":false,"sendError":false,"encoding":"utf8","allProps":false,"x":470,"y":660,"wires":[[]]},{"id":"7a9f13ccb93273a9","type":"csv","z":"5825ca337611592f","name":"","sep":";","hdrin":true,"hdrout":"none","multi":"mult","ret":"\\r\\n","temp":"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":false,"x":750,"y":660,"wires":[["0bd363b052cdb57c"]]},{"id":"0bd363b052cdb57c","type":"split","z":"5825ca337611592f","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":960,"y":660,"wires":[["86b67771cdfba8ca"]]},{"id":"12eb3e1fff0373c1","type":"http request","z":"5825ca337611592f","name":"HTTP POST","method":"GET","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"basic","x":750,"y":740,"wires":[["ccddd87c49fe1440","790a8380641e8b1f"]]},{"id":"ccddd87c49fe1440","type":"jsonpath","z":"5825ca337611592f","expression":"$..time","split":true,"name":"","x":950,"y":740,"wires":[["d5d8e3a607afa936"]]},{"id":"323e67b7ef7f22fd","type":"debug","z":"5825ca337611592f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1190,"y":820,"wires":[]},{"id":"d5d8e3a607afa936","type":"join","z":"5825ca337611592f","name":"","mode":"custom","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":";","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1010,"y":820,"wires":[["323e67b7ef7f22fd","80b345e2cfee2992"]]},{"id":"790a8380641e8b1f","type":"function","z":"5825ca337611592f","name":"","func":"msg.payload=msg.url;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":800,"y":820,"wires":[["d5d8e3a607afa936"]]},{"id":"667e5edf721dce43","type":"template","z":"5825ca337611592f","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"name;AdIP\nAA;172.24.64.18\nBB;172.24.64.48","output":"str","x":460,"y":600,"wires":[["7a9f13ccb93273a9"]]},{"id":"a312c436dbf9de12","type":"comment","z":"5825ca337611592f","name":"Lisez-Moi","info":"Créer un fichier CSV, tel le template.\nREPRENDRE ABSOLUMENT LE NOMS DES ENTETES !\n\nname;AdIP\nXXXX;XXX.XXX.XXX.XXX","x":460,"y":560,"wires":[]},{"id":"80b345e2cfee2992","type":"file","z":"5825ca337611592f","name":"","filename":"C:/Users/augus/Desktop/reponseNodeRed_DateHeure.csv","appendNewline":true,"createDir":false,"overwriteFile":"false","encoding":"none","x":1160,"y":860,"wires":[[]]},{"id":"a654952e15f7368d","type":"comment","z":"5825ca337611592f","name":"Lisez-Moi 2","info":"Créer un fichier CSV et mettre son chemin !\nAttention le fichier n'est pas éffacé entre 2 requêtes !","x":1110,"y":900,"wires":[]},{"id":"22abcb215b7b327c","type":"template","z":"5825ca337611592f","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"name;AdIP\nAA;172.24.64.18\nBB;172.24.64.48","output":"str","x":460,"y":40,"wires":[["d42eaa86.c12c18"]]},{"id":"2469c978a8abde7a","type":"comment","z":"5825ca337611592f","name":"Lisez-Moi","info":"Créer un fichier CSV, tel le template.\nREPRENDRE ABSOLUMENT LE NOMS DES ENTETES !\n\nname;AdIP\nXXXX;XXX.XXX.XXX.XXX","x":260,"y":40,"wires":[]},{"id":"16cdf1535de8caae","type":"comment","z":"5825ca337611592f","name":"Lisez-Moi 2","info":"Créer un fichier CSV et mettre son chemin !\nAttention le fichier n'est pas éffacé entre 2 requêtes !","x":1090,"y":500,"wires":[]}]
1 Like

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