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.

Simple HTTP - Tesla API
#11
Try this:

Code:
function WriteToTesla(request, request_body)
  request_url = 'https://owner-api.teslamotors.com/api/1/' .. request
  local response_body = {}
  local content_length

  if type(request_body) == "string" then
    content_length = #request_body
  else
    request_body = ""
  end

  local body, code, hdrs, stat = ssl.https.request{
    url = request_url;
    method = "POST";
    headers =
    {
      ["Content-Type"] = "application/json";
      ["Authorization"] = "Bearer " .. API_Token;
      ["Content-Length"] = content_length;
    };
    source = ltn12.source.string(request_body);
    sink = ltn12.sink.table(response_body);
  }
  if code == 200 then
    ret = table.concat(response_body)
    ret = json.pdecode(ret)
    return ret
  else
    API_Token = GetToken() -- request a new token
  end
end

result = WriteToTesla('vehicles/' .. CarID .. '/command/actuate_trunk', '{"which_trunk":"rear"}')
Reply


Messages In This Thread
Simple HTTP - Tesla API - by Mr.D - 13.12.2018, 19:35
RE: Simple HTTP - Tesla API - by Mr.D - 13.12.2018, 19:59
RE: Simple HTTP - Tesla API - by Mr.D - 14.12.2018, 15:19
RE: Simple HTTP - Tesla API - by Mr.D - 27.01.2019, 12:04
RE: Simple HTTP - Tesla API - by admin - 10.01.2020, 09:21
RE: Simple HTTP - Tesla API - by alexgleyzer - 26.05.2020, 07:38
RE: Simple HTTP - Tesla API - by admin - 26.05.2020, 08:19
RE: Simple HTTP - Tesla API - by alexgleyzer - 26.05.2020, 10:35
RE: Simple HTTP - Tesla API - by admin - 26.05.2020, 11:14
RE: Simple HTTP - Tesla API - by alexgleyzer - 27.05.2020, 06:34

Forum Jump: