REST API Usage – Arma Reforger
Category: Arma Reforger/Modding/Scripting/Tutorials
Lou Montana (talk | contribs) m (Text replacement - "\{\{GameCategory\|armaR\|Modding\|(Guidelines|Tutorials)\|([^=↵]*)\}\}" to "{{GameCategory|armaR|Modding|$2|$1}}") |
Lou Montana (talk | contribs) m (Some wiki formatting) |
||
Line 49: | Line 49: | ||
override void OnError(int errorCode) | override void OnError(int errorCode) | ||
{ | { | ||
PrintFormat("OnError(%1)", errorCode); | |||
} | } | ||
Line 61: | Line 61: | ||
override void OnSuccess(string data, int dataSize) | override void OnSuccess(string data, int dataSize) | ||
{ | { | ||
PrintFormat("OnSuccess() - data size = %1 bytes", dataSize); | |||
if (dataSize > 0) | if (dataSize > 0) | ||
Print(data); // note that Print() will not output strings longer than 1024b to console, check the dataSize! | Print(data); // note that Print() will not output strings longer than 1024b to console, check the dataSize! | ||
Line 69: | Line 69: | ||
class HttpElement | class HttpElement | ||
{ | { | ||
protected ref RestCallbackExample | protected ref RestCallbackExample m_CallbackExample; | ||
//------------------------------------------------------------------------------------------------ | //------------------------------------------------------------------------------------------------ | ||
void ExecuteRequest() | void ExecuteRequest() | ||
{ | { | ||
if (! | if (!m_CallbackExample) | ||
m_CallbackExample = new RestCallbackExample(); | |||
string contextURL = "https://httpbin.org/"; | string contextURL = "https://httpbin.org/"; | ||
Line 82: | Line 82: | ||
// executed request is "get" in "https://httpbin.org/" context (i.e "https://httpbin.org/get") | // executed request is "get" in "https://httpbin.org/" context (i.e "https://httpbin.org/get") | ||
// using the HTTP's GET method | // using the HTTP's GET method | ||
context.GET( | context.GET(m_CallbackExample, "get"); | ||
// it is possible to assemble a more complex request using arguments, like this: | // it is possible to assemble a more complex request using arguments, like this: | ||
// ctx.GET( | // ctx.GET(m_CallbackExample, "get?x=10&y=5"); | ||
} | } | ||
} | } |
Latest revision as of 16:39, 3 September 2025
The REST API (RestApi) is a simplified way to call REST requests and handle results/data, allowing to communicate with a web API. This page explains the terminology and how to use the scripting API.
Definitions
Context
The context is simply the website URL with which transactions will happen, e.g https://httpbin.org/.
A RestContext context can be created with the following:
Callback
A callback is a script class handling a request's success, error or timeout.
Once the request happens (asynchronously) the callback methods are called to process the received result.
The scripter is responsible for the callback object's lifetime. Inherit from RestCallback and implement the desired methods to create a custom callback:
Example
Declaration
class RestCallbackExample : RestCallback
{
//------------------------------------------------------------------------------------------------
override void OnError(int errorCode)
{
PrintFormat("OnError(%1)", errorCode);
}
//------------------------------------------------------------------------------------------------
override void OnTimeout()
{
Print("OnTimeout()");
}
//------------------------------------------------------------------------------------------------
override void OnSuccess(string data, int dataSize)
{
PrintFormat("OnSuccess() - data size = %1 bytes", dataSize);
if (dataSize > 0)
Print(data); // note that Print() will not output strings longer than 1024b to console, check the dataSize!
}
}
class HttpElement
{
protected ref RestCallbackExample m_CallbackExample;
//------------------------------------------------------------------------------------------------
void ExecuteRequest()
{
if (!m_CallbackExample)
m_CallbackExample = new RestCallbackExample();
string contextURL = "https://httpbin.org/";
RestContext context = GetGame().GetRestApi().GetContext(contextURL);
// executed request is "get" in "https://httpbin.org/" context (i.e "https://httpbin.org/get")
// using the HTTP's GET method
context.GET(m_CallbackExample, "get");
// it is possible to assemble a more complex request using arguments, like this:
// ctx.GET(m_CallbackExample, "get?x=10&y=5");
}
}
Call
class HolderClass
{
protected ref HttpElement m_HttpElement;
void PerformGETRequest()
{
if (!m_HttpElement)
m_HttpElement = new HttpElement();
m_HttpElement.ExecuteRequest();
}
}