How to send a json POST to remote (camera) API from Lua script?

ypwlng

New Member
Hello - I would like to send a POST to the camera API from Lua, directly if possible, on change of scene. I do not need to have the responses from the API.

Presently the workarounds I have tried are not suitable or not ideal.

1. os.execute to call Curl - there is a lag, and the scene change is delayed

2. io.popen a Python helper script; LUA ---io pipe---> Helper ---POST---> Camera API. With this it works, but there is the inconvenient cmd console window that needs to be closed/minimised

ChatGPT thinks that there is a "socket.http" library in OBS Lua - but there isn't.

I am using OBS 32.1.1 on Windows 11.

Thank you in advance for any help, suggestions, etc.
 
Hello — OBS Lua doesn’t support socket.http, so direct POST isn’t available. A simple workaround is to use a small compiled helper (e.g., Go/C) instead of Python to avoid the console window and reduce lag, or handle the POST externally via OBS WebSocket on scene change.
Thanks. I tried python compiled into an executable file, but the console window still appears. I looked briefly at OBS WebSocket - it seems to be for remote control of OBS. Are you saying that a helper file could be written to connect to Web Socket and poll for changes or listen for what OBS is doing?
 
The lpOptional (WINHTTP_NO_REQUEST_DATA) parameter from the WinHttpSendRequest function designed exactly for POST and PUT, in case you planning to modify the code from the example by yourself.
 
The lpOptional (WINHTTP_NO_REQUEST_DATA) parameter from the WinHttpSendRequest function designed exactly for POST and PUT, in case you planning to modify the code from the example by yourself.
Thank you -- I shall set myself the challenge to learn something new and modify that code.
 
I don’t think OBS Lua has built in HTTP support, so sadly ChatGPT was wrong on that one. I ran into the same issue before.

If you don’t need the response, the cleanest option might be a small background helper app or script that stays running and listens locally, then OBS just sends a quick message to it on scene change. That avoids the lag from os.execute and gets rid of the console window issue if you run it hidden.
 
Thanks. I am working on Suslik suggestion and will update here when there is success. So far it looks possible but I need to test it out on real environment to find out whether it will work better than my helper app.
 
OBS Lua doesn't have built-in HTTP support, but the ljsocket.lua I mentioned above uses FFI to interface to Windows and OSX sockets functions. The project I referenced uses it for VISCA over IP.

I have been using it for five years for PTZ camera control. My old Aver cameras just use GET with a query string. ljsocket.lua is low level: send or receive a chunk of data. I used WIreshark to see what Aver's camera interface sent to the cameras, and wrote my Lua to construct the requests.

Since you say you don't need to examine responses to your POSTs, I am guessing the stuff you need to send is simple enough that you ought to be above to construct the data streams. If you have a client app that you can spy on, Wireshark is your friend
 
ok - so as John and Suslik have mentioned - it is possible to use the ffi library, and I have cobbled together some lua code that succesfully sends POST to my camera API. (Still working on receiving the responses, just for the sake of learning a new thing).

John your ljsocket.lua as you said is more low level than using ffi to load winhttp, I'll need to take some time to understand your ljsocket.lua first.

The problem I have now is how to deal with the situation of an offline camera - because there is a long timeout somewhere in the pathway (even when I set WinHttpSetTimeouts values to small, like 100ms), and this slows down the scene changes. It seems that it will be possible to have an asynchronous call back WinHttpOpen(..., WINHTTP_FLAG_ASYNC) but this is now making it all quite complex for what I hoped would be a simple post.
 
ljsocket.lua isn't my code - I just grabbed it from the resource I mentioned above and used it for my purposes.

Timeouts for IP are usually long. Makes sense for hosts on the other side of the world; less so for a nearby camera. I didn't run into problems because I am communicating with a server running on the same PC, so timeout would only occur if the server wasn't running, in which case ALL our cameras would be down.

The problem is that ljsocket is blocking, so your send won't complete until either success or timeout. What you want is an asynchronous notification of completion, whether success or timeout. And I don't think Lua in OBS could do that.

You mentioned Websocket above. It can notify you of scene changes, so you could move the POST with asynchronous completion to a more capable environment.

You can see a Javascript example of Websocket notification of scene change in my code https://github.com/OldBaldGeek/OBS-old-bald-scripts/blob/main/cabrini-dock.js
It's part of a browser dock that we use to control our cameras from within OBS (mostly files named cabrini-dock.*)

I'm afraid that the repository is kind of a junk drawer, with scripts and such that we use to control our setup. I set it up to keep track of changes rather than intending to publish it. So there are unrelated and obsolete files. The README should give you some idea of what's there.
 
Back
Top