HTTP in Lua scripting requires command-line initialization
Summary
When using Lua HTTP functions such as comm.httpGet and comm.httpPost, by default these give the error "HTTP was not initialized, please initialize it via the command line." I was unable to find documentation on the error message, but I was able to look into the code to figure out what command-line arguments are needed. For whatever reason, these functions require a URL to be set from the command-line. Setting these via the command-line like emuhawk --url_get=. --url_post=. resolves the error and allows the HTTP functions to work properly. It doesn't seem to matter what the URLs are set to so long as they are set. Setting them within Lua using the httpSetGetUrl and httpSetPostUrl functions also does not resolve the error, these must be set in the command line.
As of right now, this issue makes it difficult to use the built-in HTTP functionality for, say, implementing online multiplayer. If this is unintentional (I don't know why these functions would require global URLs), this is a bug and should be resolved. If it is intended to require command-line initialization for security or other reasons, that should at least be documented so developers can be aware of this limitation.
Repro
- Open BizHawk normally.
- Run a Lua script using the comm.httpGet or comm.httpPost functions.
- Observe the error message.
I changed this behavior last year for a private project. It initializes HTTP regardless of command-line arguments, and instead adds an (opt-in) menu option in the Lua console. I have it working on 2.9 as well, so if there's interest I'd be happy to clean it up and submit a PR.
The entire HttpCommunication code in bizhawk seems weird to me. It apparently caches a GetUrl and a PostUrl that is used when no explicit url is provided in the function calls... but why?
I've recently been looking into the socket server API as an alternative and it seems to have some similar issues. Any socket functions will fail if either the socket IP or port is not defined via the command line. Additionally, the socket opens as soon as BizHawk starts which seems a bit odd but may be intentional as there is not an explicit function to open a socket within Lua.
我也遇到了同样的问题:HTTP was not initialized, please initialize it via the command line NLua.Exceptions.LuaScriptException: 现在有解决方案吗
Commenting to boost. Would love to be able to use comm http calls in my Lua scripts without having to initialize Emuhawk from the command line, and preferably without needing to call comm.SetPostUrl() first as well. I don't see a real reason why it's necessary to safeguard API calls so heavily, to the point where it's not really usable in a casual/silent way.