Jump to: navigation, search

Mihini/rest

REST API

Variables

local Http = require 'socket.http'
local socket = require 'socket'
local mime = require 'mime'
local ltn12 = require 'ltn12'
local sched = require 'sched'

Header Entries

h={["user-agent"]="chrome",["Content-Type"]="application/json",["Content-Language"]="en-US",
  ["content-  length"]=100}

PUT Example

local function restPUT(temp)
  result={}
  data ="{\"value\":"..temp.."}"
   --print (data)
   Jdata= ltn12.source.string(data)
 
  ok, code, head= Http.request{method="PUT", url="rest_api_url", 
                   headers=h, source = Jdata, sink=ltn12.sink.table(result)}
 end

GET Example

local function restGET()
   result={}
     local tempData=
   ok,code,head=Http.request {method="GET", url="rest_api_url"
                ,sink = ltn12.sink.table(result)  ,headers=h}
 
  for k,v in pairs(result) do
         tempData=tempData..v
   end
  return tempData
 end

Main

 local function main() 
  temp=25
  while(true) do
    temp=restGET
    restPUT(temp)
    sched.wait(3)
  end
 end
 sched.run(main)
 sched.loop()