Before starting with Storm’s RESTful API, please make sure to check Configuration – RESTful API page.
Here is an example on how to create a very simple request using PHP. Storm uses Basic Auth for authorization.
<?php
// Authentication data for Basic Auth
$username = 'example_user';
$password = 'example_password';
// URL Address for API
$apiUrl = 'http://localhost/rest/info;
// cURL session initialization
$curl = curl_init($apiUrl);
// Optional settings
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, $username . ':' . $password);
// Executing request
$response = curl_exec($curl);
// Checking for request execution errors
if ($response === false) {
$error = curl_error($curl);
echo 'Request execution error: ' . $error;
} else {
// Checking the response status
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
switch ($httpCode) {
case 200:
// Success - processing the response
$responseData = json_decode($response, true);
// Handle the response data...
break;
case 401:
// Authentication failure
echo 'Authentication failed. Access denied.';
break;
case 404:
// Invalid URL
echo 'Invalid URL. Resource not found.';
break;
case 500:
// Server-side error
echo 'Server-side error. Please try again later.';
break;
default:
// Other response codes
echo 'Unexpected response code: ' . $httpCode;
break;
}
}
// Closing the cURL session
curl_close($curl);
?>
In order to create a POST or PUT request we’ll have to specify Content-Type and add CURLOPT_CUSTOMREQUEST and POSTFIELDS fields like in this example.
Storm accepts POST data as JSON encoded strings.
// Data to send as JSON
$jsonData = json_encode($data);
// Initialize cURL session
$curl = curl_init($apiUrl);
// Set request options
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, $username . ':' . $password);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($jsonData)
));
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($curl, CURLOPT_POSTFIELDS, $jsonData);
// Execute the PUT request
$response = curl_exec($curl);
All responses are JSON-based objects. Each response contains status field, which indicated whenever operation was “success” or “failed”. Field “message” is provided whenever an error occurs.
HTTP/1.1 200 OK
Content-Type: application/json
{
"status":"success",
"data":{
"status":"running",
"startDate":1685397038
}
}
or:
HTTP/1.1 401 Unauthorized
Content-Type: application/json
{
"status":"failed",
"message":"Unknown user or password"
}
Storm’s response codes follow standard HTTP schemes:
200 | OK |
401 | Authentication failure |
404 | Invalid URL |
500 | Server-side error |