Jump to content


Photo

JSON Functionality for Addons


    No replies to this topic

    #1 isil

    isil

      Newbie

    • Members
    • Pip
    • 3 posts

      Posted 07 November 2016 - 04:58 PM

      In case anyone is planning to work with JSON, here is an example on sending POST requests via HTTP to a JSON server as e,g, https://hub.docker.c...ue/json-server/ .

      local http = require("socket.http")
      local ltn12 = require("ltn12")
      
      function sendRequest()
      	local server = "http://10.0.0.10:8080/oseem/"
      	local payload = [[ {"name":"My Name","description":"The description","state":1} ]]
      	local response_body = { }
      
      	local res, code, response_headers, status = http.request
      	{
      	url = server,
      	method = "POST",
      	headers =
      	{
      		["Content-Type"] = "application/json",
      		["Content-Length"] = payload:len()
      	},
      	source = ltn12.source.string(payload),
      	sink = ltn12.sink.table(response_body)
      	}
      end
      
      windower.register_event("incoming text", function(original,modified,original_mode,modified_mode,blocked)
      	if original_mode == 12 or original_mode == 4 then --tell
      		sendRequest()
      	end
      end)
      

       






      1 user(s) are reading this topic

      0 members, 1 guests, 0 anonymous users