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

**URL:** <https://discourse.nodered.org/t/error-cannot-set-headers-after-they-are-sent-to-the-client/54527>\
**Category:** General\
**Created:** [30 November 2021 19:14 UTC](https://discourse.nodered.org/t/error-cannot-set-headers-after-they-are-sent-to-the-client/54527 "2021-11-30T19:14:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![wjr1506](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wjr1506/32/50619_2.png) [@wjr1506](https://discourse.nodered.org/u/wjr1506)\
**Post date:** [30 November 2021 19:14 UTC](https://discourse.nodered.org/t/error-cannot-set-headers-after-they-are-sent-to-the-client/54527/1 "2021-11-30T19:14:22Z")

</div>

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.

```auto
[{"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?

 ![1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/2/625d7aa3bfc86263667cd14c864fac0a8400ddeb.png)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [30 November 2021 20:38 UTC](https://discourse.nodered.org/t/error-cannot-set-headers-after-they-are-sent-to-the-client/54527/2 "2021-11-30T20:38:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![wjr1506](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wjr1506/32/50619_2.png) [@wjr1506](https://discourse.nodered.org/u/wjr1506)\
**Post date:** [1 December 2021 05:14 UTC](https://discourse.nodered.org/t/error-cannot-set-headers-after-they-are-sent-to-the-client/54527/3 "2021-12-01T05:14:26Z")

</div>

> [@E1cid](#):
>
> 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`

 ![1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/9/e998f06ef188c298e472c3586bf5d255c2d0e966.png)

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/c/3cf14888bd7f72ac79a5a49228fe0f1b456e6df9.png)

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

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [1 December 2021 10:35 UTC](https://discourse.nodered.org/t/error-cannot-set-headers-after-they-are-sent-to-the-client/54527/4 "2021-12-01T10:35:45Z")

</div>

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.

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

```

---

<div class="post-metadata">

**Author:** ![wjr1506](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wjr1506/32/50619_2.png) [@wjr1506](https://discourse.nodered.org/u/wjr1506)\
**Post date:** [1 December 2021 12:22 UTC](https://discourse.nodered.org/t/error-cannot-set-headers-after-they-are-sent-to-the-client/54527/5 "2021-12-01T12:22:25Z")

</div>

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!

---

<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 December 2021 12:23 UTC](https://discourse.nodered.org/t/error-cannot-set-headers-after-they-are-sent-to-the-client/54527/6 "2021-12-15T12:23:09Z")

</div>

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