# HTTP Request to get access through an API

**URL:** <https://discourse.nodered.org/t/http-request-to-get-access-through-an-api/60705>\
**Category:** General\
**Created:** [3 April 2022 04:14 UTC](https://discourse.nodered.org/t/http-request-to-get-access-through-an-api/60705 "2022-04-03T04:14:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![astrojav](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/astrojav/32/58738_2.png) [@astrojav](https://discourse.nodered.org/u/astrojav)\
**Post date:** [3 April 2022 04:14 UTC](https://discourse.nodered.org/t/http-request-to-get-access-through-an-api/60705/1 "2022-04-03T04:14:45Z")

</div>

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.

```auto
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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/4/44edd11bc6f173c19b264560f3efcd09e24fdd9a.png)

**The flow:**

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

```

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

**The Inject node structure looks like this:**

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

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [3 April 2022 04:50 UTC](https://discourse.nodered.org/t/http-request-to-get-access-through-an-api/60705/2 "2022-04-03T04:50:09Z")

</div>

> 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:

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

```

---

<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:** [3 April 2022 08:09 UTC](https://discourse.nodered.org/t/http-request-to-get-access-through-an-api/60705/3 "2022-04-03T08:09:11Z")

</div>

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.

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

```

---

<div class="post-metadata">

**Author:** ![astrojav](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/astrojav/32/58738_2.png) [@astrojav](https://discourse.nodered.org/u/astrojav)\
**Post date:** [3 April 2022 20:18 UTC](https://discourse.nodered.org/t/http-request-to-get-access-through-an-api/60705/4 "2022-04-03T20:18:37Z")

</div>

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:

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

```

---

<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:** [3 April 2022 20:52 UTC](https://discourse.nodered.org/t/http-request-to-get-access-through-an-api/60705/5 "2022-04-03T20:52:30Z")

</div>

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

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

```

```auto
{
   "PASS": $$.Password,
   "U": $round($millis()/1000),
   "T": $$.payload.ambient,
   "H": $$.payload.humidity,
   "P": $$.payload.pressure,
   "W": $$.payload.windspd,
   "B": $$.payload.winddir 
}

```

---

<div class="post-metadata">

**Author:** ![astrojav](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/astrojav/32/58738_2.png) [@astrojav](https://discourse.nodered.org/u/astrojav)\
**Post date:** [4 April 2022 18:33 UTC](https://discourse.nodered.org/t/http-request-to-get-access-through-an-api/60705/6 "2022-04-04T18:33:18Z")

</div>

Hi @E1cid,

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

Best regards.

---

<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 April 2022 18:33 UTC](https://discourse.nodered.org/t/http-request-to-get-access-through-an-api/60705/7 "2022-04-18T18:33:32Z")

</div>

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