This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

API Basic Auth
#1
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.
Reply
#2
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
Reply
#3
(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.
Reply
#4
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.
Reply
#5
(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>
Reply
#6
You need to use ssl.https instead of socket.http:

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


Forum Jump: