# Condition based http response

**URL:** <https://discourse.nodered.org/t/condition-based-http-response/55481>\
**Category:** General\
**Tags:** http-request, function-node, mqtt\
**Created:** [20 December 2021 14:23 UTC](https://discourse.nodered.org/t/condition-based-http-response/55481 "2021-12-20T14:23:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jinesh](https://avatars.discourse-cdn.com/v4/letter/j/3e96dc/32.png) [@jinesh](https://discourse.nodered.org/u/jinesh)\
**Post date:** [20 December 2021 14:23 UTC](https://discourse.nodered.org/t/condition-based-http-response/55481/1 "2021-12-20T14:23:13Z")

</div>

On http in node we are sending a request to the MQTT broker using mqtt out node and on MQTT in node response we need to trigger the HTTP response.

we tried with below but doesnt seem to work,  
can anyone please help me with this

` [{"id":"fddbf80b980767ff","type":"mqtt in","z":"5e7c412c8b50ab24","name":"","topic":"in/topic1","qos":"2","datatype":"auto","broker":"9dcfb076.0d327","nl":false,"rap":true,"rh":0,"inputs":0,"x":180,"y":260,"wires":[["725f7f15da51da50"]]},{"id":"f10aa70b8d84e061","type":"mqtt out","z":"5e7c412c8b50ab24","name":"","topic":"out/topic1","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"9dcfb076.0d327","x":580,"y":100,"wires":[]},{"id":"aabd5ea787546e9d","type":"http in","z":"5e7c412c8b50ab24","name":"","url":"/getdata","method":"get","upload":false,"swaggerDoc":"","x":200,"y":100,"wires":[["f10aa70b8d84e061","725f7f15da51da50"]]},{"id":"2861f2a6e8074be2","type":"http response","z":"5e7c412c8b50ab24","name":"","statusCode":"","headers":{},"x":770,"y":180,"wires":[]},{"id":"725f7f15da51da50","type":"function","z":"5e7c412c8b50ab24","name":"","func":"if(msg.payload != null) \n{\n msg.statusCode=200;\n}\nelse\n{\n msg.statusCode=500;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":520,"y":180,"wires":[["2861f2a6e8074be2"]]},{"id":"eb7f4d41c1652939","type":"comment","z":"5e7c412c8b50ab24","name":"","info":"On recieveing the HTTP request and its respective respone from MQTT in node , \nwe want to send the HTTP response code as 200 with some payload data or else 500\n\nand this process should repeat for all the http in requests","x":510,"y":240,"wires":[]},{"id":"9dcfb076.0d327","type":"mqtt-broker","name":"local","broker":"127.0.0.1","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"5","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"true","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]`

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [20 December 2021 14:42 UTC](https://discourse.nodered.org/t/condition-based-http-response/55481/2 "2021-12-20T14:42:24Z")

</div>

As described in the help text for the http request node, the message sent to the response node must contain `msg.res` as generated by the request node. So you must include that in the data passed to mqtt and then pick it up again when you get it back from mqtt.

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [20 December 2021 16:24 UTC](https://discourse.nodered.org/t/condition-based-http-response/55481/3 "2021-12-20T16:24:10Z")

</div>

This is a little tricky because to solve this you have to somehow wait for the mqtt-in node's reply and then as Colin said pass the `msg.res` (and the `msg.req` ?) to the http-response to complete the request/response chain.

Usually you use a Join node to achieve this .. wait for 2 x msgs, merge them and only then send to  
http-response but i didnt manage to get it to work this way.

so i thought of using some Javascript to create a `setInterval` to monitor when a reply from the mqtt-in is saved in flow Context and then send the msg to http-response.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/d/8db93da3539bf93e320e567abb644081f81f28d6.png)

**Here is a test flow:**

```auto
[{"id":"68efb1a6e926fe14","type":"mqtt in","z":"54efb553244c241f","name":"","topic":"in/topic1","qos":"2","datatype":"json","broker":"9dcfb076.0d327","nl":false,"rap":true,"rh":0,"inputs":0,"x":240,"y":760,"wires":[["5c3197b364cc0942"]]},{"id":"721ad3b2d592b8d5","type":"mqtt out","z":"54efb553244c241f","name":"","topic":"out/topic1","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"9dcfb076.0d327","x":580,"y":520,"wires":[]},{"id":"3a076ee9cba4d241","type":"http in","z":"54efb553244c241f","name":"","url":"/getdata","method":"get","upload":false,"swaggerDoc":"","x":290,"y":520,"wires":[["721ad3b2d592b8d5","8c7be3a037d4ed3a","679f8de88a4d59aa"]]},{"id":"73817ab0b5bfea95","type":"http response","z":"54efb553244c241f","name":"","statusCode":"","headers":{"content-type":"application/json"},"x":890,"y":620,"wires":[]},{"id":"8c7be3a037d4ed3a","type":"debug","z":"54efb553244c241f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":490,"y":460,"wires":[]},{"id":"52f6e1cc6a67633f","type":"debug","z":"54efb553244c241f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":610,"y":760,"wires":[]},{"id":"d1dcd218c664595a","type":"debug","z":"54efb553244c241f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":890,"y":680,"wires":[]},{"id":"679f8de88a4d59aa","type":"function","z":"54efb553244c241f","name":"monitor reply","func":"node.status({ fill: \"green\", shape: \"dot\", text: \"Waiting mqtt..\" }); // set Node status\n\n// timer to check for mqtt reply\nlet timer = setInterval(() => {\n\n let mqttReply = flow.get(\"mqttReply\")\n if (mqttReply) {\n msg.payload = mqttReply // attach mqttReply to payload\n flow.set(\"mqttReply\", undefined) // clear flow Context\n node.send(msg); // send msg\n node.status({}); // clear status\n clearInterval(timer) // self clear timer\n\n }\n}, 250)\n\nreturn null;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":590,"y":620,"wires":[["73817ab0b5bfea95","d1dcd218c664595a"]]},{"id":"5c3197b364cc0942","type":"function","z":"54efb553244c241f","name":"set Flow Context","func":"flow.set(\"mqttReply\", msg.payload)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":430,"y":760,"wires":[["52f6e1cc6a67633f"]]},{"id":"9dcfb076.0d327","type":"mqtt-broker","name":"local","broker":"127.0.0.1","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"5","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"true","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]

```

---

<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:** [20 December 2021 16:53 UTC](https://discourse.nodered.org/t/condition-based-http-response/55481/4 "2021-12-20T16:53:49Z")

</div>

Should be possible to use a join  
here is an example

```auto
[{"id":"3a076ee9cba4d241","type":"http in","z":"c791cbc0.84f648","name":"","url":"/getdata","method":"get","upload":false,"swaggerDoc":"","x":120,"y":1320,"wires":[["8c7be3a037d4ed3a","fc500b45.3094e","722d2d8c.d1cccc"]]},{"id":"8c7be3a037d4ed3a","type":"debug","z":"c791cbc0.84f648","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":340,"y":1260,"wires":[]},{"id":"fc500b45.3094e","type":"change","z":"c791cbc0.84f648","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"in","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":1360,"wires":[["1bb2d2b.5fa732d"]]},{"id":"722d2d8c.d1cccc","type":"change","z":"c791cbc0.84f648","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$random()*100","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":1320,"wires":[["721ad3b2d592b8d5"]]},{"id":"1bb2d2b.5fa732d","type":"join","z":"c791cbc0.84f648","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":510,"y":1380,"wires":[["73817ab0b5bfea95","d1dcd218c664595a"]]},{"id":"721ad3b2d592b8d5","type":"mqtt out","z":"c791cbc0.84f648","name":"","topic":"out/topic1","qos":"","retain":"","broker":"e8ba3ef5.22f4a8","x":540,"y":1320,"wires":[]},{"id":"e2553edb.6846f","type":"change","z":"c791cbc0.84f648","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"mqtt","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":290,"y":1420,"wires":[["1bb2d2b.5fa732d"]]},{"id":"73817ab0b5bfea95","type":"http response","z":"c791cbc0.84f648","name":"","statusCode":"","headers":{"content-type":"application/json"},"x":690,"y":1360,"wires":[]},{"id":"d1dcd218c664595a","type":"debug","z":"c791cbc0.84f648","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":670,"y":1420,"wires":[]},{"id":"68efb1a6e926fe14","type":"mqtt in","z":"c791cbc0.84f648","name":"","topic":"out/topic1","qos":"2","datatype":"json","broker":"e8ba3ef5.22f4a8","x":90,"y":1420,"wires":[["e2553edb.6846f"]]},{"id":"dbdfdb57.97bfd8","type":"inject","z":"c791cbc0.84f648","name":"test mqtt in","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"test\":1}","payloadType":"json","x":140,"y":1560,"wires":[["d2c987bb.d82608"]]},{"id":"d2c987bb.d82608","type":"http request","z":"c791cbc0.84f648","name":"","method":"GET","ret":"txt","paytoqs":"query","url":"http://192.168.1.25:1880/getdata","tls":"","persist":false,"proxy":"","authType":"","x":340,"y":1560,"wires":[["62dacdbd.ae3fd4"]]},{"id":"62dacdbd.ae3fd4","type":"debug","z":"c791cbc0.84f648","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":550,"y":1560,"wires":[]},{"id":"e8ba3ef5.22f4a8","type":"mqtt-broker","name":"testb","broker":"192.168.1.25","port":"1883","clientid":"node-red","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

```

Unborn I think your mqtt is not using same topic.

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [20 December 2021 17:05 UTC](https://discourse.nodered.org/t/condition-based-http-response/55481/5 "2021-12-20T17:05:09Z")

</div>

> [@E1cid](#):
>
> Unborn I think your mqtt is not using same topic

I know .. only because Jinesh had a different topic in his flow .. in my tests i changed it like yours.

Nice solution by the way .. you got it working .. much cleaner than using timers

---

<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:** [18 February 2022 17:05 UTC](https://discourse.nodered.org/t/condition-based-http-response/55481/6 "2022-02-18T17:05:54Z")

</div>

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