Guest User

RPC4Minetest

a guest
Aug 29th, 2017
72
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function rpc.call(url, method, ...)
  2.   local JSONRequestArray = {
  3.     id=tostring(math.random()),
  4.     ["method"]=method,
  5.     params = ...
  6.   }
  7.   local jsonRequest = minetest.write_json(JSONRequestArray)
  8.   local httpResponse = http.fetch_async_get(
  9.     http.fetch_async(
  10.         { ['url'] = url,
  11.           method = 'POST',
  12.           extra_headers = { ['content-type']='text/plain', ['content-length']=string.len(jsonRequest) },
  13.           post_data = jsonRequest
  14.        }
  15.     )
  16.   )
  17.   -- Check the http response code
  18.   if (httpResponse.code~=200) then
  19.      return nil, "HTTP ERROR: " .. httpResponse.code
  20.   end
  21.  
  22.   return minetest.parse_json(httpResponse.data).result, nil
  23. end
RAW Paste Data