(Last Updated: April 27, 2020)

Lua example http post request with json body



  • -- https request  json body
    -- Enable potentially unsafe lua code in,  user account - security
    local https = require "ssl.https"
    local http = require "socket.http"
    local ltn12 = require "ltn12"
    local json = require "dkjson"
    local payload = {{"test", "test", null, 0}}  --payload to be sent
    
    local request_body = { secret = "1.KAddd87s8s8FasL7-ze4KG_p5hrVg4Kee1-t3vnwGFhI=", to = "test@gmail.com", device = null, priority = "high", payload = payload } --the json body
    local response_body = {}
    request_body = json.encode(request_body)
    
    local r, c, h, s = https.request {
      url = 'https://llamalab.com/automate/cloud/message',
      method = 'POST',
      headers = {
        ["Content-Type"] = "application/json",
        ["Content-Length"] = string.len(request_body)
      },
      source = ltn12.source.string(request_body),
      sink = ltn12.sink.table(response_body)
    }
    
    

Log in to reply