"Error: Cannot set headers after they are sent to the client"

Hello everyone!
I have a question, is there any way to loop a flow that has an http node?

I'm trying to integrate a chatbot created in Watson Assistan into instagram. As I didn't find a specific node for sending messages by Direct, I decided to get the data through the http Get protocol.

[{"id":"4dda54daa8841532","type":"tab","label":"INSTAGRAM","disabled":false,"info":"","env":[]},{"id":"5546a833c1a5323e","type":"change","z":"4dda54daa8841532","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"req.query.mensage","tot":"msg"},{"t":"set","p":"resAux","pt":"flow","to":"res","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":200,"y":100,"wires":[["b152bacba5dc37e1","4dcc76f29cff783e"]]},{"id":"b152bacba5dc37e1","type":"watson-assistant-v2","z":"4dda54daa8841532","name":"Albert (RADinfo-instagram)","service-endpoint":"https://api.us-south.assistant.watson.cloud.ibm.com/instances/f7d07c5e-8056-456e-99d5-ac64a4c0c2d9","assistant_id":"a28ada9a-8b42-4af5-9f57-73e99b4fb6e3","debug":true,"restart":false,"return_context":true,"alternate_intents":true,"multisession":false,"timeout":"","optout-learning":false,"persist-session-id":true,"x":160,"y":160,"wires":[["9bfd44296a217a6f","406f87d944b24edf"]]},{"id":"c1fb16f8efde2437","type":"change","z":"4dda54daa8841532","name":"","rules":[{"t":"set","p":"res","pt":"msg","to":"resAux","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":210,"y":340,"wires":[["89622ac688d5d57a","da31ac5c4351818d"]]},{"id":"89622ac688d5d57a","type":"http response","z":"4dda54daa8841532","name":"","statusCode":"","headers":{},"x":230,"y":400,"wires":[]},{"id":"e8dff131ec26e51c","type":"change","z":"4dda54daa8841532","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.output.generic","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":200,"y":280,"wires":[["c1fb16f8efde2437","c1ae4f4a3cb99006"]]},{"id":"9bfd44296a217a6f","type":"function","z":"4dda54daa8841532","name":"","func":"msg.payload = msg.payload.output.generic;\nvar msgMsg = msg.payload;\nvar req = msg.req;\nvar res = msg.res;\nvar size = msg.payload.length;\nnewMsg = [];\n\nfor (var index = 0; index < size; index++){\n    var resType = msg.payload[index].response_type;\n    \n        if (resType == \"text\"){\n        newMsg.push({\n            payload:\n                {\n                content:msgMsg[index].text,\n                response_type: resType,\n                },\n                req: req,\n                res: res\n            \n        })\n    }\n    else {\n        newMsg.push({\n            payload:\n                {\n\n                    content:msgMsg[index].title,\n                    response_type: resType,\n                },\n                req: req,\n                res: res\n        })\n    }\n\n    \n}\n\n\nreturn [newMsg];","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":220,"y":220,"wires":[["e8dff131ec26e51c","aeb05e536099c1a4"]]},{"id":"3d5e986c22dc8a33","type":"http in","z":"4dda54daa8841532","name":"get-radinfo-insta","url":"/instagram","method":"get","upload":false,"swaggerDoc":"","x":200,"y":40,"wires":[["5546a833c1a5323e","f07a8945b16137d3"]]},{"id":"da31ac5c4351818d","type":"debug","z":"4dda54daa8841532","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":390,"y":340,"wires":[]},{"id":"c1ae4f4a3cb99006","type":"debug","z":"4dda54daa8841532","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":390,"y":280,"wires":[]},{"id":"aeb05e536099c1a4","type":"debug","z":"4dda54daa8841532","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":390,"y":220,"wires":[]},{"id":"406f87d944b24edf","type":"debug","z":"4dda54daa8841532","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":390,"y":160,"wires":[]},{"id":"4dcc76f29cff783e","type":"debug","z":"4dda54daa8841532","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":390,"y":100,"wires":[]},{"id":"f07a8945b16137d3","type":"debug","z":"4dda54daa8841532","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":390,"y":40,"wires":[]}]

I'm calling JSON from node red in chaftuel, and on that platform there's no way to manipulate Objects or Arrays.

So, the way I thought was to make a loop to read each Array, but I bumped into this question, the request doesn't work and returns this error. "Error: Cannot set headers after they are sent to the client".

Is there anything that can be done?

The error comes from sending Multiple messages to the http response node. It can only return one, the first.

Hard to say what to do, will need more info, an example of payload.output.generic and an explanation of how it's contents is supposed to be return.

got it, well, in that case, I can get the JSON from this URL:https://node-red-yrbsr-2021-11-03.mybluemix.net/instagram?mensage=message-here

He is currently in Portuguese-BR. But when passing a value to the variable, it returns that JSON in full.
And what I really wanted is for it to return one Array at a time.

Something close to that image.

image

The responses sent would be in Arrays that would be read and sent separately

As I don't know what your response requires i can only guess. You need to handle the array and return all if in some form, in one response.
here is an example that parses the array and returns some of the info. you just need to return it in correct format you need.

[{"id":"89622ac688d5d57a","type":"http response","z":"4dda54daa8841532","name":"","statusCode":"","headers":{},"x":250,"y":260,"wires":[]},{"id":"9bfd44296a217a6f","type":"function","z":"4dda54daa8841532","name":"","func":"msg.payload = msg.payload.output.generic;\nvar msgMsg = msg.payload;\nvar size = msgMsg.length;\nmsg.payload ={}\n\nfor (var index = 0; index < size; index++){\n    var resType = msgMsg[index].response_type;\n    \n        if (resType == \"text\"){\n            msg.payload[\"message\"] = msgMsg[index].text;\n                \n                \n            \n    \n    } else if (resType == \"option\"){\n            msg.payload.title = msgMsg[index].title;\n            msg.payload.options = {};\n            msgMsg[index].options.forEach(e => {\n                    msg.payload.options[e.label] = e.value.input.text;\n                })\n\n    }\n    \n\n    \n}\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":220,"y":200,"wires":[["aeb05e536099c1a4","89622ac688d5d57a"]]},{"id":"aeb05e536099c1a4","type":"debug","z":"4dda54daa8841532","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":390,"y":200,"wires":[]},{"id":"4cf9f267.c48c4c","type":"change","z":"4dda54daa8841532","name":"","rules":[{"t":"set","p":"payload.output.generic","pt":"msg","to":"[{\"response_type\":\"text\",\"text\":\"🤔 Certo,  então me informe se quer *vídeo* do  funcionamento  *transferir* para consultor ou saber como *corrigir* ?\"},{\"title\":\"\",\"options\":[{\"label\":\"Vídeo de treinamento\",\"value\":{\"input\":{\"text\":\"Video\"}}},{\"label\":\"Corrigir um problema\",\"value\":{\"input\":{\"text\":\"Corrigir\"}}},{\"label\":\"Transferir p/ consultor\",\"value\":{\"input\":{\"text\":\"Transferir\"}}}],\"description\":\"\",\"response_type\":\"option\"}]","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":160,"y":140,"wires":[["9bfd44296a217a6f","406f87d944b24edf"]]},{"id":"5546a833c1a5323e","type":"change","z":"4dda54daa8841532","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"req.query.mensage","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":210,"y":80,"wires":[["4dcc76f29cff783e","4cf9f267.c48c4c"]]},{"id":"406f87d944b24edf","type":"debug","z":"4dda54daa8841532","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":390,"y":140,"wires":[]},{"id":"4dcc76f29cff783e","type":"debug","z":"4dda54daa8841532","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":390,"y":80,"wires":[]},{"id":"3d5e986c22dc8a33","type":"http in","z":"4dda54daa8841532","name":"get-radinfo-insta","url":"/instagram","method":"get","upload":false,"swaggerDoc":"","x":200,"y":20,"wires":[["5546a833c1a5323e","f07a8945b16137d3"]]},{"id":"f07a8945b16137d3","type":"debug","z":"4dda54daa8841532","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":390,"y":20,"wires":[]}]
1 Like

Actually, it works in debug, but it doesn't support in chatbot. I believe I'll have to create a script to receive this get, loop and send the message straight to instagram

thank you very much for your time!

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