HTTP request node - How to insert a variable in the url path

How to insert a variable in the url path

Hello guys!
I'm trying to build a dashboard for creating dynamic extensions on the asterisk using the REST of the asterisk itself, however the final object of the URL path is the extension number that will be informed on the dashboard, so it is necessary to pass this information to the URL field of the node http request.
I have tried it via query string but I have certainly done something wrong if it is possible to send this information over there.
Follow the print with the URL I need to assemble: (<USER> is just a highlight where I want to insert the variable with the extension number.)

[{"id":"41bdb42.b505b4c","type":"ui_form","z":"aaf61169.b76f3","name":"Auth","label":"Dados do UsuĆ”rio","group":"c264c8ca.7c1ef8","order":0,"width":0,"height":0,"options":[{"label":"Ramal","value":"username","type":"number","required":true,"rows":null},{"label":"Senha","value":"password","type":"password","required":true,"rows":null}],"formValue":{"username":"","password":""},"payload":"","submit":"Salvar","cancel":"Cancelar","topic":"userpass","x":170,"y":260,"wires":[["49dca02c.dd386"]]},{"id":"49dca02c.dd386","type":"function","z":"aaf61169.b76f3","name":"Auth","func":"flow.set('user', msg.payload.username);\nvar data_user = flow.get('user');\nvar data_pswd = msg.payload.password;\nmsg.headers = {};\nmsg.headers['Content-Type'] = 'application/json';\nmsg.payload = {\n    \"fields\": [\n        {\n            \"attribute\": \"auth_type\",\n            \"value\": \"userpass\"\n        },\n        {\n            \"attribute\": data_user,\n            \"value\": data_pswd\n        },\n        {\n            \"attribute\": \"nonce_lifetime\",\n            \"value\": \"32\"\n        }\n        ]\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":310,"y":220,"wires":[["24ca682.8cde198"]]},{"id":"24ca682.8cde198","type":"http request","z":"aaf61169.b76f3","name":"PUT Auth","method":"PUT","ret":"txt","paytoqs":"ignore","url":"http://127.0.0.1:8088/ari/asterisk/config/dynamic/res_pjsip/auth/+data_user+{{{query}}}","tls":"","persist":false,"proxy":"","authType":"digest","x":460,"y":160,"wires":[["e3dbd495.d4cb78"]]},{"id":"e3dbd495.d4cb78","type":"debug","z":"aaf61169.b76f3","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":610,"y":80,"wires":[]},{"id":"c264c8ca.7c1ef8","type":"ui_group","name":"AutenticaĆ§Ć£o","tab":"f1ecfcfd.21cc3","order":1,"disp":true,"width":"6","collapse":false},{"id":"f1ecfcfd.21cc3","type":"ui_tab","name":"ConfiguraĆ§Ć£o de Ramais","icon":"dialer_sip","disabled":false,"hidden":false}]```

it looks like you are already using {{{query}}} - mustache syntax - to insert msg.query so why not carry on and use {{{user}}} and prior to this node set up msg.user - or indeed whatever you may have assigned already.

1 Like

Hello Friend

I confess that I am new to learning node red and with that I don't know how to manipulate some information.
This is the flow I'm testing:

[{"id":"41bdb42.b505b4c","type":"ui_form","z":"aaf61169.b76f3","name":"Auth","label":"Dados do UsuĆ”rio","group":"c264c8ca.7c1ef8","order":0,"width":0,"height":0,"options":[{"label":"Ramal","value":"username","type":"number","required":true,"rows":null},{"label":"Senha","value":"password","type":"password","required":true,"rows":null}],"formValue":{"username":"","password":""},"payload":"","submit":"Salvar","cancel":"Cancelar","topic":"userpass","x":170,"y":260,"wires":[["49dca02c.dd386"]]},{"id":"49dca02c.dd386","type":"function","z":"aaf61169.b76f3","name":"Auth","func":"flow.set('user', msg.payload.username);\nvar data_user = flow.get('user');\nvar data_pswd = msg.payload.password;\nmsg.headers = {};\nmsg.headers['Content-Type'] = 'application/json';\nmsg.payload = {\n    \"fields\": [\n        {\n            \"attribute\": \"auth_type\",\n            \"value\": \"userpass\"\n        },\n        {\n            \"attribute\": data_user,\n            \"value\": data_pswd\n        },\n        {\n            \"attribute\": \"nonce_lifetime\",\n            \"value\": \"32\"\n        }\n        ]\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":310,"y":220,"wires":[["24ca682.8cde198"]]},{"id":"24ca682.8cde198","type":"http request","z":"aaf61169.b76f3","name":"PUT Auth","method":"PUT","ret":"txt","paytoqs":"ignore","url":"http://127.0.0.1:8088/ari/asterisk/config/dynamic/res_pjsip/auth/+data_user+{{{query}}}","tls":"","persist":false,"proxy":"","authType":"digest","x":460,"y":160,"wires":[["e3dbd495.d4cb78"]]},{"id":"e3dbd495.d4cb78","type":"debug","z":"aaf61169.b76f3","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":610,"y":80,"wires":[]},{"id":"c264c8ca.7c1ef8","type":"ui_group","name":"AutenticaĆ§Ć£o","tab":"f1ecfcfd.21cc3","order":1,"disp":true,"width":"6","collapse":false},{"id":"f1ecfcfd.21cc3","type":"ui_tab","name":"ConfiguraĆ§Ć£o de Ramais","icon":"dialer_sip","disabled":false,"hidden":false}]

Where would I put the entry that the friend suggested?

Sorry - I don't know enough about Asterisk to know if that form of PUT will work .
Certainly you aren't creating msg.query in your function so that is redundant as part of the url. You need to check the Asterisk API to find out what it really needs to look like before you can attempt to make your data look like it. As the form directly gives you msg.payload.username then based on your first request I would expect {{{payload.username}}} - fed directly from the form input (without the function node) - would be what you wanted - but I suspect that won't be correct until we have all the pieces of what is really required.

2 Likes

Thanks again Friend!

The query works well when I insert in the URL the last object in the path which is the extension number.

Example: http://127.0.0.1:8088/ari/asterisk/config/dynamic/res_pjsip/auth/1000{{{query}}}

This works perfectly!
But what I really need is that the "1000" which is the last part of the URL path can be passed as a variable or even within the query, because in place of "1000" it will always be 'msg.payload. username '.
I apologize for not being able to explain clearly, but what I'm trying to know is how to pass this information on the url path, as the query in this case represents the json body of the request.

Follow asterisk reference:

Does the url without {{{query}}} work?
http://127.0.0.1:8088/ari/asterisk/config/dynamic/res_pjsip/auth/1000.
If so,
try
http://127.0.0.1:8088/ari/asterisk/config/dynamic/res_pjsip/auth/{{{the_msg_containing_user}}}

Hello Friend!

It does not work without {{{query}}}, as it is in the query that the body with the extension attributes goes.
I went a little further by defining the URL with msg.url thus being able to dynamically pass the extension using msg.payload.username!
But now I crashed the part of sending the query ... lol

Here is the model that works perfect using imput:

[{"id": "52e47de9.68f864", "type": "inject", "z": "aaf61169.b76f3", "name": "Auth", "props": [{"p": "payload "}]," repeat ":" "," crontab ":" "," once ": false," onceDelay ": 0.1," topic ":" "," payload ":" {\ "fields \": [ {\ "attribute \": \ "auth_type \", \ "value \": \ "userpass \"}, {\ "attribute \": \ "username \", \ "value \": \ "1002 \" }, {\ "attribute \": \ "password \", \ "value \": \ "1231231 \"}, {\ "attribute \": \ "md5_cred \", \ "value \": \ "\ "}]}", "payloadType": "json", "x": 130, "y": 80, "wires": [["5ea01a56.1b82b4"]]}}, {"id": "5ea01a56.1b82b4 "," type ":" function "," z ":" aaf61169.b76f3 "," name ":" Standard "," func ":" msg.headers = {}; \ nmsg.headers ['Content-Type' ] = 'application / json'; \ nreturn msg; "," outputs ": 1," noerr ": 0," initialize ":" "," finalize ":" "," x ": 280," y ": 80, "wires": [["c08c1dce.8eda1"]]}, {"id": "c08c1dce.8eda1", "type": "http request", "z": "aaf61169.b76f3", "name" : "PUT Auth", "method": "PUT", "ret": "txt", "paytoqs": "ignore", "url": "", "tls": "", "persist": false, "proxy": "", "authType": "digest", "x": 440, "y": 80, "wires": [["e3dbd495.d4cb78"]]}}, {"id": "e3dbd495. d4cb78 "," type ":" deb ug "," z ":" aaf61169.b76f3 "," name ":" "," active ": true," tosidebar ": true," console ": false," tostatus ": false," complete ":" false "," statusVal ":" "," statusType ":" auto "," x ": 610," y ": 80," wires ": []}]

This is the model using the form imput via dashboard:

[{"id": "41bdb42.b505b4c", "type": "ui_form", "z": "aaf61169.b76f3", "name": "Auth", "label": "User Data", "group ":" c264c8ca.7c1ef8 "," order ": 0," width ": 0," height ": 0," options ": [{" label ":" Extension "," value ":" username "," type ":" number "," required ": true," rows ": null}, {" label ":" Password "," value ":" password "," type ":" password "," required ": true, "rows": null}], "formValue": {"username": "", "password": ""}, "payload": "", "submit": "Save", "cancel": "Cancel" , "topic": "userpass", "x": 170, "y": 260, "wires": [["49dca02c.dd386"]]}, {"id": "49dca02c.dd386", "type" : "function", "z": "aaf61169.b76f3", "name": "Auth", "func": "flow.set ('user', msg.payload.username); \ nvar data_user = flow.get ('user'); \ nvar data_pswd = msg.payload.password; \ nmsg.headers = {}; \ nmsg.headers ['Content-Type'] = 'application / json'; \ nmsg.payload = {\ n \ "fields \": [\ n {\ n \ "attribute \": \ "auth_type \", \ n \ "value \": \ "userpass \" \ n}, \ n {\ n \ "attribute \ ": data_user, \ n \" value \ ": data_pswd \ n}, \ n {\ n \ "attribute \": \ "nonce_lifetime \", \ n \ "value \": \ "32 \" \ n} \ n] \ n}; \ nmsg.query = msg.payload; \ nmsg. url = 'http://127.0.0.1:8088/ari/asterisk/config/dynamic/res_pjsip/auth/'+data_user+'';\nreturn msg; "," outputs ": 1," noerr ": 0," initialize ":" "," finalize ":" "," x ": 310," y ": 220," wires ": [[" 24ca682.8cde198 "]]}, {" id ":" 24ca682.8cde198 " , "type": "http request", "z": "aaf61169.b76f3", "name": "PUT Auth", "method": "PUT", "ret": "txt", "paytoqs": " ignore "," url ":" "," tls ":" "," persist ": false," proxy ":" "," authType ":" digest "," x ": 460," y ": 160, "wires": [["e3dbd495.d4cb78"]]}}, {"id": "e3dbd495.d4cb78", "type": "debug", "z": "aaf61169.b76f3", "name": "" , "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "false", "statusVal": "", "statusType": "auto", "x ": 610," y ": 80," wires ": []}, {" id ":" c264c8ca.7c1ef8 "," type ":" ui_group "," name ":" Authentication "," tab ":" f1ecfcfd.21cc3 "," order ": 1," disp ": true," width ":" 6 "," collapse ": false}, {" id ":" f1ecfcfd.21cc3 "," type ":" ui_tab " , "name": "Configuration extension number "," icon ":" dialer_sip "," disabled ": false," hidden ": false}]