POST request for PushBullet

Hi everyone,

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:

r: 1     
c: 403     
h: table: 0x207ff20     
s: HTTP/1.1 403 Forbidden 

Which is to be expected.

What am I doing wrong?

Any help would very much be appreciated!

Thanks in advance,

Anil

request_body = json.encode(body) doesn’t look right. Perhaps. request_body = json.encode(request_body)

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

Hi Buxton,

Thanks for the reply. You are right, I had the wrong variable name, but it is giving me the same results.

The API_CODE was changed for my post, so I didn’t post the actual API key.

Your code works for me at another site that uses https is your access token correct?

from document at Pushbullet API

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 

You have Authorization

Hi ElCid,

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?

Anil

Thats where i tested from to.

Edit/ Use luup.log() to log response vars to the log. As print does not work in the test luup tool.

Then use http://your_-vera_ip/cgi-bin/cmh/log.sh?Device=LuaUPnP to view log in browser or use LuaView app form vera apps.

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.

The LuaTest page does support print statements but I will test with luup.log as well.

Hi Buxton,

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.