Sending a body for http request outside the node

Hi,

I want to send a http request but I need to send a body with it. This I want to do with a template node.
But when I send it, it doesn't work.



You have not given enough info to give a definite answer, If this is a post request then the payload needs to be an object not a text string, try setting the template to output a object.

How do you mean?

where is says output as.


You did not show the mustache format in original post, all info is important

select format mustache, above where it says output.

[{"id":"3c2ab2fc9d965430","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"set","p":"headers.Authorization","pt":"msg","to":"\"Bearer \" & $globalContext(\"UserMatrix[5][1]\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":1480,"wires":[["2817e15eff895b41","7e817af038cbae64"]]},{"id":"c0935bad310557da","type":"template","z":"bf9e1e33.030598","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{\"mode\": \"NORMAL\",\"limit\": {\"unit\": \"PERCENTAGE\", \"value\": {{payload}}}}\n\n","output":"json","x":280,"y":1480,"wires":[["3c2ab2fc9d965430"]]},{"id":"2817e15eff895b41","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":850,"y":1560,"wires":[]},{"id":"7e817af038cbae64","type":"http request","z":"bf9e1e33.030598","name":"Laadpaal sturen %","method":"PUT","ret":"txt","paytoqs":"body","url":"https://app1pub.smappee.net/dev/v3/chargingstations/6000000046/connectors/1/mode","tls":"","persist":false,"proxy":"","authType":"","x":770,"y":1480,"wires":[["2817e15eff895b41"]]},{"id":"a368235a87847f26","type":"ui_slider","z":"bf9e1e33.030598","name":"","label":"Laadpercentage","tooltip":"","group":"58d1cfd1a2aec4cf","order":4,"width":3,"height":1,"passthru":true,"outs":"end","topic":"laadpercentage","min":0,"max":"100","step":"10","x":100,"y":1480,"wires":[["c0935bad310557da"]]},{"id":"dc6b3fb7.f44678","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":180,"y":1480,"wires":[["c0935bad310557da"]]},{"id":"58d1cfd1a2aec4cf","type":"ui_group","name":"Connector 1","tab":"56b7e433f1483e4d","order":1,"disp":true,"width":"6","collapse":false},{"id":"56b7e433f1483e4d","type":"ui_tab","name":"Laadpalen sturen","icon":"dashboard","order":10,"disabled":false,"hidden":true}]

is payload a number? a string?

Use a debug node BEFORE the template node to check the value of msg.payload

image

that looks normal. something else must be wrong.

Can you share your flow (only the parts that matter)

PS, sending screenshots of JSON is not good for us to help you (as we cannot copy & paste screenshots into a code editor to check)

This is my flow, I think that you need the token: a733c03b-a21e-3d95-94f1-ecf782696280

[{"id":"2817e15eff895b41","type":"debug","z":"fd77b4dafda3312f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":950,"y":300,"wires":[]},{"id":"7e817af038cbae64","type":"http request","z":"fd77b4dafda3312f","name":"Laadpaal sturen %","method":"PUT","ret":"txt","paytoqs":"body","url":"https://app1pub.smappee.net/dev/v3/chargingstations/6000000046/connectors/1/mode","tls":"","persist":false,"proxy":"","authType":"","senderr":true,"credentials":{},"x":770,"y":300,"wires":[["2817e15eff895b41"]]},{"id":"3c2ab2fc9d965430","type":"change","z":"fd77b4dafda3312f","name":"","rules":[{"t":"set","p":"headers.Authorization","pt":"msg","to":"\"Bearer \" & $globalContext(\"UserMatrix[5][1]\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":300,"wires":[["7e817af038cbae64"]]},{"id":"c0935bad310557da","type":"template","z":"fd77b4dafda3312f","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"plain","template":"{\"mode\": \"NORMAL\",\"limit\": {\"unit\": \"PERCENTAGE\", \"value\": {{msg.payload}}}}\n\n","output":"str","x":280,"y":300,"wires":[["3c2ab2fc9d965430"]]},{"id":"a368235a87847f26","type":"ui_slider","z":"fd77b4dafda3312f","name":"","label":"Laadpercentage","tooltip":"","group":"58d1cfd1a2aec4cf","order":4,"width":3,"height":1,"passthru":true,"outs":"end","topic":"laadpercentage","min":0,"max":"100","step":"10","x":100,"y":300,"wires":[["c0935bad310557da","80082c6a63545408"]]},{"id":"58d1cfd1a2aec4cf","type":"ui_group","name":"Connector 1","tab":"56b7e433f1483e4d","order":1,"disp":true,"width":"6","collapse":false},{"id":"56b7e433f1483e4d","type":"ui_tab","name":"Laadpalen sturen","icon":"dashboard","order":10,"disabled":false,"hidden":true}]

Here you go...

[{"id":"2817e15eff895b41","type":"debug","z":"c2233fc1d8cc5c3b","name":"Success (200)","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1520,"y":800,"wires":[]},{"id":"7e817af038cbae64","type":"http request","z":"c2233fc1d8cc5c3b","name":"Laadpaal sturen %","method":"PUT","ret":"txt","paytoqs":"body","url":"https://app1pub.smappee.net/dev/v3/chargingstations/6000000046/connectors/1/mode","tls":"","persist":false,"proxy":"","authType":"","senderr":true,"x":1130,"y":840,"wires":[["01741bf269003147"]]},{"id":"3c2ab2fc9d965430","type":"change","z":"c2233fc1d8cc5c3b","name":"","rules":[{"t":"set","p":"headers.Authorization","pt":"msg","to":"\"Bearer a733c03b-a21e-3d95-94f1-ecf782696280\"","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":870,"y":840,"wires":[["7e817af038cbae64","9443b01bae04a5f3"]]},{"id":"a368235a87847f26","type":"ui_slider","z":"c2233fc1d8cc5c3b","name":"","label":"Laadpercentage","tooltip":"","group":"58d1cfd1a2aec4cf","order":4,"width":3,"height":1,"passthru":true,"outs":"end","topic":"laadpercentage","min":0,"max":"100","step":"10","x":460,"y":840,"wires":[["afb2087fedf6d848"]]},{"id":"11b2d6767a37219f","type":"debug","z":"c2233fc1d8cc5c3b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":770,"y":920,"wires":[]},{"id":"9443b01bae04a5f3","type":"debug","z":"c2233fc1d8cc5c3b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1078,"y":905,"wires":[]},{"id":"17aa846d816458e9","type":"inject","z":"c2233fc1d8cc5c3b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"10","payloadType":"num","x":470,"y":900,"wires":[["afb2087fedf6d848"]]},{"id":"8f716df58bff1065","type":"inject","z":"c2233fc1d8cc5c3b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"20","payloadType":"num","x":480,"y":940,"wires":[["afb2087fedf6d848"]]},{"id":"afb2087fedf6d848","type":"function","z":"c2233fc1d8cc5c3b","name":"","func":"msg.payload = { \n    \"mode\": \"NORMAL\", \n    \"limit\": { \n        \"unit\": \"PERCENTAGE\", \n        \"value\": msg.payload \n        }\n}\nconst UserMatrix = global.get(\"UserMatrix\");\n\n//enable next line to use global UserMatrix\n//const token = UserMatrix[5][1];\n\n//delete next line to use global UserMatrix\nconst token = \"a733c03b-a21e-3d95-94f1-ecf782696280\"\n\n\nmsg.headers = {\n    Authorization: `Bearer ${token}`\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":640,"y":840,"wires":[["3c2ab2fc9d965430","11b2d6767a37219f"]]},{"id":"01741bf269003147","type":"switch","z":"c2233fc1d8cc5c3b","name":"","property":"statusCode","propertyType":"msg","rules":[{"t":"eq","v":"200","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1330,"y":840,"wires":[["2817e15eff895b41"],["97e0aec4dbb32104"]]},{"id":"97e0aec4dbb32104","type":"debug","z":"c2233fc1d8cc5c3b","name":"Problem - check msg.statusCode","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1580,"y":860,"wires":[]},{"id":"58d1cfd1a2aec4cf","type":"ui_group","name":"Connector 1","tab":"56b7e433f1483e4d","order":1,"disp":true,"width":"6","collapse":false},{"id":"56b7e433f1483e4d","type":"ui_tab","name":"Laadpalen sturen","icon":"dashboard","order":10,"disabled":false,"hidden":true}]

Be sure to check inside the function node.

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