BizHawk icon indicating copy to clipboard operation
BizHawk copied to clipboard

HTTP in Lua scripting requires command-line initialization

Open MarioManTAW opened this issue 2 years ago • 5 comments

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

  1. Open BizHawk normally.
  2. Run a Lua script using the comm.httpGet or comm.httpPost functions.
  3. Observe the error message.

MarioManTAW avatar May 21 '23 02:05 MarioManTAW

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.

emuhawk_luahttp_option_screenshot_20230526_194930

EddieRingle avatar May 27 '23 00:05 EddieRingle

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?

Morilli avatar Jun 07 '23 21:06 Morilli

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.

MarioManTAW avatar Jun 25 '23 00:06 MarioManTAW

我也遇到了同样的问题:HTTP was not initialized, please initialize it via the command line NLua.Exceptions.LuaScriptException: 现在有解决方案吗

80491636 avatar Sep 14 '23 08:09 80491636

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.

quitemason avatar Nov 13 '25 03:11 quitemason