Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function rpc.call(url, method, ...)
- local JSONRequestArray = {
- id=tostring(math.random()),
- ["method"]=method,
- params = ...
- }
- local httpResponse, result , code
- local jsonRequest = minetest.write_json(JSONRequestArray)
- -- We use the sophisticated http.request form (with ltn12 sources and sinks) so that
- -- we can set the content-type to text/plain. While this shouldn't strictly-speaking be true,
- -- it seems a good idea (Xavante won't work w/out a content-type header, although a patch
- -- is needed to Xavante to make it work with text/plain)
- local ltn12 = require('ltn12')
- local resultChunks = {}
- httpResponse, code = http.request(
- { ['url'] = url,
- sink = ltn12.sink.table(resultChunks),
- method = 'POST',
- headers = { ['content-type']='text/plain', ['content-length']=string.len(jsonRequest) },
- source = ltn12.source.string(jsonRequest)
- }
- )
- httpResponse = table.concat(resultChunks)
- -- Check the http response code
- if (code~=200) then
- return nil, "HTTP ERROR: " .. code
- end
- -- And decode the httpResponse and check the JSON RPC result code
- result = minetest.parse_json( httpResponse )
- if result.result then
- return result.result, nil
- else
- return nil, result.error
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement