I’m exploring a plugin system and experimenting with Einhugur’s LuaScript plugin. I’m trying to make a function to make HTTP requests in Lua. The function will get passed to Xojo, which will use URLConnection to perform the actual work.
So far, my very simple Lua script looks like:
response = httpRequest('GET', 'https://api.url.com/', {Authentication='password'})
print(response)
The challenge I’m running into is iterating over the table in the third parameter. My context object’s IsTable(3) returns true. But the only examples I can find for iterating a table are for a table stored in a global named variable. How do I do this with a passed parameter?
My Xojo code looks like this so far:
Var Context As LuaScriptContext = New LuaScriptContext(ContextPointer)
If Context.ParameterCount < 2 Then
Context.SetError("httpRequest needs at least requestMethod and url parameters.")
Return 0
End If
Var RequestMethod As String = Context.GetString(1, False)
Var Url As String = Context.GetString(2, False)
Var Conn As New URLConnection
If Context.ParameterCount > 2 And Context.IsTable(3) Then
// Headers included
// What now?
End If
Try
Var Response As String = Conn.SendSync(RequestMethod, Url)
Context.Push(Response)
Catch Err As RuntimeException
Context.SetError(Err.Message)
End Try
Return 1
On a side note, JavaScript was my first choice, especially since I already own the MBS plugins. But the included DukTape javascript engine doesn’t support any of the modern features my users will expect, and JSContextMBS has no browser context, so that’s also missing important things like the fetch
function. Python is not self-contained and package management would be an issue. PHPMBS is deprecated, and if I recall correctly, wasn’t self-contained either. Oh and XojoScript is effectively useless due to the small userbase, and no decent IDE. I’m open to other ideas, but I’m struggling to come up with others. So Lua it is if I can make this work.