HTTP Request to get access through an API

Hi folks,

I'm trying to pass some sensor values to an 'api.php' file as a HTTP request that is located on a remote web server. The request should be accepted by the 'api.php' using a _GET variable. I am trying to do this from node-red with the flow below.
Unfortunately, the request is sent but the API returns "No password provided".
When I enter the password into a browser as shown below it will return "Success". I'm not sure if I'm sending the HTTP request from node-red as it should.

Any help is much appreciated.

Here's an example of a request URL that should be sent to the 'api.php' to pass the sensor values.

http://www.meteotemplate.com/template/api.php?U=1486255070&T=25.3&H=79.9&P=1010.1&W=5.6&G=7.8&R=0&RR=0&B=272&S=788&PASS=password123

image

The flow:

[{"id":"fdb1688a952261c8","type":"tab","label":"Flow 2","disabled":false,"info":"","env":[]},{"id":"dfdd6bff9cfd8e90","type":"inject","z":"fdb1688a952261c8","name":"","props":[{"p":"headers.content-type","v":"application/x-www-form-urlencoded","vt":"str"},{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"PASS=2ngT25CsDipY&U=1648939207069&T=23.5&H=60&P=1005.14&W=0&B=0","payloadType":"str","x":370,"y":220,"wires":[["359689d7cdbaf3d0","a972f3c9a3e86ce3"]]},{"id":"a972f3c9a3e86ce3","type":"http request","z":"fdb1688a952261c8","name":"","method":"POST","ret":"txt","paytoqs":"body","url":"https://skynstars.com/template/api.php?","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"x":550,"y":220,"wires":[["c37929e0b6c8579c"]]},{"id":"359689d7cdbaf3d0","type":"debug","z":"fdb1688a952261c8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":550,"y":180,"wires":[]},{"id":"c37929e0b6c8579c","type":"debug","z":"fdb1688a952261c8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":730,"y":220,"wires":[]}]

The Inject node structure looks like this:

using a _GET variable

But you are using POST in the http request node ?

You can use the full url in the http request node as a GET request (the same as in the browser) and it will work.

example:

[{"id":"dfdd6bff9cfd8e90","type":"inject","z":"fdb1688a952261c8","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":248,"y":216,"wires":[["a972f3c9a3e86ce3"]]},{"id":"a972f3c9a3e86ce3","type":"http request","z":"fdb1688a952261c8","name":"","method":"GET","ret":"txt","paytoqs":"ignore","url":"https://skynstars.com/template/api.php?PASS=2ngT25CsDipY&U=1648939207069&T=23.5&H=60&P=1005.14&W=0&B=0","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"x":410,"y":216,"wires":[["c37929e0b6c8579c"]]},{"id":"c37929e0b6c8579c","type":"debug","z":"fdb1688a952261c8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":578,"y":216,"wires":[]}]

You can also pass the query string parameters in the msg.payload, and the http request block can attach them to the url
e.g.

[{"id":"dfdd6bff9cfd8e90","type":"inject","z":"fdb1688a952261c8","name":"","props":[{"p":"headers.content-type","v":"application/x-www-form-urlencoded","vt":"str"},{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"PASS\":\"2ngT25CsDipY\",\"U\":1648939207069,\"T\":23.5,\"H\":60,\"P\":1005.14,\"W\":0,\"B\":0}","payloadType":"json","x":370,"y":220,"wires":[["359689d7cdbaf3d0","a972f3c9a3e86ce3"]]},{"id":"359689d7cdbaf3d0","type":"debug","z":"fdb1688a952261c8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":550,"y":180,"wires":[]},{"id":"a972f3c9a3e86ce3","type":"http request","z":"fdb1688a952261c8","name":"","method":"GET","ret":"txt","paytoqs":"query","url":"https://skynstars.com/template/api.php","tls":"","persist":false,"proxy":"","authType":"","x":550,"y":220,"wires":[["c37929e0b6c8579c"]]},{"id":"c37929e0b6c8579c","type":"debug","z":"fdb1688a952261c8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":730,"y":220,"wires":[]}]

Hi @E1cid

Thanks for the prompt response.
How can I change the string to include a UNIX format timestamp and sensor data?
I have some sensor data in msg.payload.ambient for "T", msg.payload.ambient for "H", msg.payload.pressure for "P", msg.payload.windspd for "W" and msg.payload.winddir for "B".

For example this JSONata expression:

{
   "U": Timestamp,
   "T": msg.payload.ambient,
   "H": msg.payload.humidity,
   "P": msg.payload.pressure,
   "W": msg.payload.windspd,
   "B": msg.payload.winddir
}

If you need a UNIX timestamp in seconds you would need to divide the JSONata $millis() by 1000 and round it.
e.g.

[{"id":"dfdd6bff9cfd8e90","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"headers.content-type","v":"application/x-www-form-urlencoded","vt":"str"},{"p":"payload"},{"p":"Password","v":"2ngT25CsDipY","vt":"str"},{"p":"Timestamp","v":"","vt":"date"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"ambient\":23.5,\"humidity\":60,\"pressure\":1005.14,\"windspd\":0,\"winddir\":0}","payloadType":"json","x":150,"y":2600,"wires":[["bffa8f2a.fd8ee"]]},{"id":"bffa8f2a.fd8ee","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\t   \"PASS\": $$.Password,\t   \"U\": $round($millis()/1000),\t   \"T\": $$.payload.ambient,\t   \"H\": $$.payload.humidity,\t   \"P\": $$.payload.pressure,\t   \"W\": $$.payload.windspd,\t   \"B\": $$.payload.winddir \t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":200,"y":2660,"wires":[["359689d7cdbaf3d0","a972f3c9a3e86ce3"]]},{"id":"359689d7cdbaf3d0","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":390,"y":2620,"wires":[]},{"id":"a972f3c9a3e86ce3","type":"http request","z":"bf9e1e33.030598","name":"","method":"GET","ret":"txt","paytoqs":"query","url":"https://skynstars.com/template/api.php","tls":"","persist":false,"proxy":"","authType":"","x":390,"y":2660,"wires":[["c37929e0b6c8579c"]]},{"id":"c37929e0b6c8579c","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":570,"y":2660,"wires":[]}]
{
   "PASS": $$.Password,
   "U": $round($millis()/1000),
   "T": $$.payload.ambient,
   "H": $$.payload.humidity,
   "P": $$.payload.pressure,
   "W": $$.payload.windspd,
   "B": $$.payload.winddir 
}
1 Like

Hi @E1cid,

This was the solution I was looking for, thanks much for the help.

Best regards.

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