I am new to Lua so please bear with me, I know I am doing something silly - I just need another pair of eyes.
I am trying to create a new PushBullet message using a POST request, however nothing is being returned by the server. If I change the https to http I get access denied errors, but with https I just get nil outputted from all of my variables.
My code is:
local https = require "ssl.https"
local http = require "socket.http"
local ltn12 = require "ltn12"
local json = require "dkjson"
local request_body = { type = "note", title = "title", body = "body", device_iden = "DEVICE_CODE" }
local response_body = {}
request_body = json.encode(request_body)
local r, c, h, s = https.request {
url = 'https://api.pushbullet.com/v2/pushes',
method = 'POST',
headers = {
["Content-Type"] = "application/json",
["Content-Length"] = string.len(request_body),
["Authorization"] = "Bearer API_CODE"
},
source = ltn12.source.string(request_body),
sink = ltn12.sink.table(response_body)
}
print('r: ' .. tostring(r))
print('c: ' .. tostring(c))
print('h: ' .. tostring(h))
print('s: ' .. tostring(s))
My output is:
r: nil
c: nil
h: nil
s: nil
But when I change https.request to http.request I get:
Also. “Bearer API_CODE” is being used as a literal string, not a variable. You have to lose the quotes if you want to pass a variable ie
"Bearer " … API_CODE
To authenticate for the API, use your access token in a header like Access-Token: <your_access_token_here>. Your access token can be found on the Account Settings page. Keep in mind that this key has full access to your
Thanks for confirming the code works for you. The token is correct and works perfectly through Postman to send the request. I have tried using the Access-Token header and also the Authorization header which works in Postman, but no joy.
It is strange that I am getting no response at all. I am using the LuaTest tool on Vera, could that have something to do with it?
Depending on the type of Vera you’re using, you may have to install a lua https/SSL package. I use lua rocks for my installs, as the installer takes care of most dependencies, but you should check the forum to read up on what’s involved as I’m sure this sort of thing has been written about before, particularly for https.
I have Vera Edge and currently use VeraAlerts plugin for PushBullet notifications. As VeraAlerts works and PushBullet only supports https I assume all the packages required are already installed.
Best Home Automation shopping experience. Shop at Ezlo!