HTTP Request Handling

Have your plugin handle HTTP requests sent to a special URL.

Plugin Code

This code only works on the Server side files since HTTP only works on servers, obviously!

You must return something, even if it's just a confirmation the data was received. Otherwise, the HTTP request will hang and eventually error on the client's side.

RegisterPluginHttpEvent("yourpluginname:hello", function(data)
    debugLog(("Got data: %s"):format(json.encode(data)))
    return { result = "ok, got some data!" }
end)

Breakdown

The framework exposes the RegisterPluginHttpEvent function for plugins to use (is not exported, currently). Using this function "registers" a particular type where the framework will send the POSTed payload to the callback function.

Note: This will block the HTTP request, so take care if this is requesting data from an external source like a database.

To actually use this, we would POST to the URL, as noted below.

Return a LUA table which will be encoded as JSON back to the requesting client.

Plugin Event

POST http://yourserverip:serverport/sonorancad/pluginevent

Send a payload to trigger a specific HTTP event that was defined in the plugin code.

Request Body

NameTypeDescription

data

object

A JSON object containing the payload to be sent

type

string

The registered type this data should be sent to

key

string

Your API Key

Response varies based on return statement defined in the handler.

Last updated