Logic Machine Forum
API Basic Auth - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: API Basic Auth (/showthread.php?tid=2183)



API Basic Auth - Krstfr2k - 05.08.2019

Hello, I was wondering if anyone could help me with a script for API basic authentication (REST API).
I have only recieved a username, password and a specific URL for the request, which returns a table in JSON format.

I have tested the request in Postman and it works fine, I have looked at the Tesla API script and other examples but I am having trouble understanding how to do the request properly in LUA.

This is the response from the request in Postman:


Code:
[
  {
    "number": "205",
    "statuses": [
      {
        "status": "2",
        "changeDate": "2019-08-05"
      }
    ]
  },
  {
    "number": "515",
    "statuses": [
      {
        "status": "2",
        "changeDate": "2019-08-05"
      }
    ]
  }
]


I have to access "Number" (Room number) and "Status" (HVAC State) in the response and send it to an internal storage variable or something to compare it to the previous recieved value.


RE: API Basic Auth - admin - 05.08.2019

Basic auth can be specified directly in URL: http://USER:PASS@HOST/PATH?ARG1=VAL1&ARG2=VAL2
This might not work is password has '@' or ':' characters


RE: API Basic Auth - Krstfr2k - 05.08.2019

(05.08.2019, 09:53)admin Wrote: Basic auth can be specified directly in URL: http://USER:PASS@HOST/PATH?ARG1=VAL1&ARG2=VAL2
This might not work is password has '@' or ':' characters

I have tried it this way but I could not get it to work. The username/password only contains a series of 32 numbers/letters.
I managed to get a response from the LM API without issues this way.


RE: API Basic Auth - admin - 05.08.2019

Do you get HTTP 401 error or something else? Some web servers require a specific header and/or header value to work. You can try running a local web server together with Wireshark to check the difference between LM and Postman requests.


RE: API Basic Auth - Krstfr2k - 05.08.2019

(05.08.2019, 10:37)admin Wrote: Do you get HTTP 401 error or something else? Some web servers require a specific header and/or header value to work. You can try running a local web server together with Wireshark to check the difference between LM and Postman requests.

Code:
* string: <html>
<head><title>400 The plain HTTP request was sent to HTTPS port</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<center>The plain HTTP request was sent to HTTPS port</center>
</body>
</html>



RE: API Basic Auth - admin - 05.08.2019

You need to use ssl.https instead of socket.http:

Code:
https = require('ssl.https')
res, err = https.request('https://...')