http.sendRequestSends an HTTP request to the specified URL.
Signature: http.sendRequest(url string, method string, body any = nil, headers Map = nil) Response
Arguments:
url — The URL to which the request will be sent.method — The HTTP method. Valid values: GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS.body — The body of the request. Can be a scalar value, a list, or an associative array.headers — HTTP request headers. The default header is Content-Type: application/json.Return value: HTTP response object (Response).
Example of use:
// Make a request to get a list of users
$response = http.sendRequest(“https://iam.twin-ai.com/api/v1/users”, “GET”, {“limit”: 15, “offset”: 5}, {‘Authorization’: “Bearer authToken”})
// Get info about the request
$statusCode = $response.statusCode
$body = $response.body
$headers = $response.headers
$error = $response.error
For json or xml content types, if the body value is a list or associative array, it is automatically converted to a string of the appropriate format.
The same happens with the response body: if the response content is in json or xml format, the body is automatically converted to a list or associative array.
// JSON request body in the form of BPL data structures
$requestBody = [
{
“param1”: “value1”,
“param2”: 123,
},
{
“param3”: [“a”, ‘b’, “c”],
},
]
// This body will be automatically converted to a string
$response = http.sendRequest(“http://some.url”, ‘POST’, $requestBody, {“Content-Type”: “application/json”})
// If the response content type is json, the body will be automatically converted to a BPL structure
$body = $response.body
// XML request body as BPL data structures
$requestBody = [
{
“tag”: “xml”,
“value”: [
{
“tag”: “head”,
“value”: [
{
“tag”: “title”,
“value”: “chapter 2”,
},
],
},
{
“tag”: “body”,
“value”: [
{
“tag”: “div”,
“attributes”: {
“class”: “color”,
“style”: “display:block;”,
},
“value”: [
“Some”,
{
“tag”: “br”,
},
“text”,
],
},
{
“tag”: “div”,
“attributes”: {
“id”: “a12”,
},
“value”: [
{
“tag”: “i”,
“value”: “Another text”,
},
{
“tag”: “span”,
“expand”: true, // if true, then a tag with an empty value will have a closing tag
},
],
},
],
},
],
},
]
// This body will be automatically converted to a string
$response = http.sendRequest(“http://some.url”, ‘POST’, $requestBody, {“Content-Type”: “text/xml”})
// If the response content type is xml, the body will be automatically converted to a BPL structure
$body = $response.body
http.requestForms a new HTTP request object.
Signature: http.request(url string = “”, method string = “POST”, body any = nil) Request
Arguments:
url — URL string.method — name of the HTTP method.body — content of the request body.Return value: An object containing information about the HTTP request.
Example of use:
$response = http.request(“https://some.url”, “POST”, {‘param1’: 123, “param2”: true}).
headers({“Content-Type”: “application/json”}).
timeout(300).
send()
twin.setApiKeysSets the authorization keys for requests to the TWIN API. Saves the values to the system variables @apiKey and @apiSecretKey.
This function simplifies the authorization process for requests to the TWIN API sent using the twin.request and twin.sendRequest functions. When using these functions, you will not need to specify an authorization header.
Signature: twin.setApiKeys(apiKey str, apiSecretKey str) void
Arguments:
apiKey — access token.apiSecretKey — refresh token.Result: The keys are saved to system variables and will be automatically used when making requests via twin.sendRequest.
Example of use:
twin.setApiKeys(“abc123”, “refresh456”)
twin.requestForms an HTTP request object for accessing the TWIN API. Equivalent to the http.request function.
Signature: twin.request(url string = “”, method string = “POST”, body any = nil) Request
Arguments:
url — request address.method — HTTP method, e.g. GET, POST, PUT, DELETE.body — request body. Can be a string, dictionary, or list.Result: A request object that can be further configured (headers, timeout, etc.) and passed to twin.sendRequest.
Example of use:
$request = twin.request("https://api.twin24.ai/v1/users", "GET").
timeout(300).
send()
twin.sendRequestSends an HTTP request to the TWIN API. Equivalent to the http.sendRequest function.
Signature: twin.sendRequest(url string, method string, body any = nil, headers map = nil) Response
Arguments:
url — request address.method — HTTP method. Acceptable values: GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS.body — request content. Scalars, lists, and dictionaries are supported. For json and xml, the body is automatically serialized.headers — dictionary of HTTP headers. Content-Type: application/json is used by default.Result: Response object containing:
statusCode — response code.body — response content (automatically converted for json/xml).headers — response headers.error — error message (if any).Example of use:
$response = twin.sendRequest("https://api.twin24.ai/v1/users", "GET", {"limit": 15}, {"Content-Type": "application/json"})
$status = $response.statusCode
$users = $response.body
For json or xml content:
If the request body is a structure (list or dictionary), it will be automatically converted to a string of the required format. The response is also converted back to a BPL data structure.