A new Flexera Community experience is coming on November 25th. Click here for more information.
This article shows practical examples on how to use the different features of the Web API in coded projects. These examples are written in C# and use third party libraries like RestSharp as rest client.
A token is required for authentication to the Spider Web API, which must be included in every request.
The authentication token can be obtained via a GET request using basic authentication. The token is then returned in the server response header.
private string GetUserTokenExample() {
// The Url of the Spider Web API
var yourApiUrl = "http://yourWebServer/SpiderWebApi";
// Spider Username
var username = "YourSpiderUsername";
//Spider Password
var password = "YourSpiderPassword";
// Create a BasicAuthenticator RestClient with your API Url, user and Password
// The rest Client is an restsharp Client in the example
RestClient client = new RestClient {
BaseUrl = new Uri(yourApiUrl),
Authenticator = new HttpBasicAuthenticator(username, password)
};
// Create a Rest request and pass the API resource Name 'token'
RestRequest request = new RestRequest {Resource = "token"};
// Optional, trust the security certificate
ServicePointManager.ServerCertificateValidationCallback +=
(sender, certificate, chain, sslPolicyErrors) => true;
// Send request to the Web API
IRestResponse response = client.Execute<HttpResponseMessage>(request);
// Optional: Exception handling
if (response.ErrorException != null){
const string message = "Error retrieving response. Check inner details for more info.";
var yourException = new ApplicationException(message, response.ErrorException);
throw yourException;
}
// Get the token from the headers of the response
string yourSpiderAPiToken = "";
if (response?.Headers == null) return "";
// The token is located in a http Header with name 'Token'
foreach (Parameter header in response.Headers.ToList())
switch (header?.Name){
case "Token":
if (header.Value != null) yourSpiderAPiToken = header.Value.ToString();
break;
default: continue;
}
//Return the Token
return yourSpiderAPiToken;
}
The entity scope enables data operations such as creating, reading, modifying and deleting entities (CRUD).
private void GetEntityExample(string token)
{
// The url of mandator you from the web api help page
var url = "{mandatorid}/entity/asset/{entityid}";
// example 0/entity/Asset/1
// The Url of the Spider Web API
var yourApiUrl = "http://yourWebServer/SpiderWebApi";
// the token you have from the first Token example, and you will use this token in every request
// Create a Rest Clien
RestClient client = new RestClient {BaseUrl = new Uri(yourApiUrl)};
// Initialize your Rest Request
RestRequest request = new RestRequest
{
Resource = url,
RequestFormat = DataFormat.Json
};
//Format Header
request.AddHeader("Accept", "application/json, text/json");
//Authentication add your token as an Authentication to your request object.
request.AddHeader("Authorization", "Token " + token);
//Optional, Trust the security certificate
//ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
//Send the Request
IRestResponse response = client.Execute(request);
// Optional: Exception handling
if (response.ErrorException != null)
{
const string message = "Error retrieving response. Check inner details for more info.";
var yourException = new ApplicationException(message, response.ErrorException);
throw yourException;
}
}
private void CreateEntityExample(string token)
{
// The url of mandator you from the web api help page
var url = "{mandatorid}/entity/{entity}/";
// example 0/entity/tablet
// json to create the asset
//{
// "fields":
// {
// "hostname":"Test Hostname",
// "Mandator":0,
// "assetNo": "Test no",
// "name": "Test Asset name",
// "assetStatusID": 1,
// "commentStatus": "comment test",
// "serialNo": "123456",
// "modelName": "Latitude E6320",
// }
//}
// Escape the " with \ in your create entity json
var jsonToSend = "{\"fields\":\r\n {\r\n\"hostname\":\"Test Hostname\",\r\n\"Mandator\":0,\r\n\"assetNo\": \"Test no\",\r\n\"name\": \"Test Asset name\",\r\n\"assetStatusID\": 1,\r\n\"commentStatus\": \"comment test\",\r\n\"serialNo\": \"123456\",\r\n\"modelName\": \"Latitude E6320\",\r\n}\r\n}";
// The Url of the Spider Web API
var yourApiUrl = "http://yourWebServer/SpiderWebApi";
// the token you have from the first Token example, and you will use this token in every request
// Create a Rest Client
RestClient client = new RestClient {BaseUrl = new Uri(yourApiUrl)};
// Initialize your Rest Request set the http post method
RestRequest request = new RestRequest
{
Resource = url,
RequestFormat = DataFormat.Json,
Method = Method.POST
};
//Format Header
request.AddHeader("Accept", "application/json, text/json");
//Authentication add your token as an Authentication to your request object.
request.AddHeader("Authorization", "Token " + token);
//Optional, Trust the security certificate
//ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
//Set the body of the request
if (!string.IsNullOrEmpty(jsonToSend))
{
request.AddParameter("application/json; charset=utf-8", jsonToSend, ParameterType.RequestBody);
}
//Send the Request
IRestResponse response = client.Execute(request);
// Optional: Exception handling
if (response.ErrorException != null)
{
const string message = "Error retrieving response. Check inner details for more info.";
var yourException = new ApplicationException(message, response.ErrorException);
throw yourException;
}
}
private void UpdateEntityExample(string token)
{
// The url of mandator you from the Web API help page
var url = "{mandatorid}/entity/{entity}/";
// example 0/entity/tablet
// Escape the " with \ in your update entity json
var jsonToSend = " {\"fields\":{\"hostname\":\" New Hostname\"}";
// The Url of the Spider Web API
var yourApiUrl = "http://yourWebServer/SpiderWebApi";
// the token you have from the first Token example, and you will use this token in every request
// Create a Rest Client
RestClient client = new RestClient {BaseUrl = new Uri(yourApiUrl)};
// Initialize your Rest Request set the http post metho
RestRequest request = new RestRequest
{
Resource = url,
RequestFormat = DataFormat.Json,
Method = Method.PUT
};
//Format Header
request.AddHeader("Accept", "application/json, text/json");
//Authentication add your token as an Authentication to your request object.
request.AddHeader("Authorization", "Token " + token);
//Optional, Trust the security certificate
//ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
//Set the body of the request
if (!string.IsNullOrEmpty(jsonToSend))
{
request.AddParameter("application/json; charset=utf-8", jsonToSend, ParameterType.RequestBody);
}
//Send the Request
IRestResponse response = client.Execute(request);
// Optional: Exception handling
if (response.ErrorException != null)
{
const string message = "Error retrieving response. Check inner details for more info.";
var yourException = new ApplicationException(message, response.ErrorException);
throw yourException;
}
}
private void DeleteEntityExample(string token)
{
// The url of mandator you from the web api help page
var url = "{mandatorid}/entity/asset/{entityid}";
// example 0/entity/Asset/1
// The Url of the Spider Web API
var yourApiUrl = "http://yourWebServer/SpiderWebApi";
// Create a Rest Client
RestClient client = new RestClient {BaseUrl = new Uri(yourApiUrl)};
// Initialize your Rest Request
RestRequest request = new RestRequest
{
Resource = url,
RequestFormat = DataFormat.Json,
Method = Method.DELETE
};
//Format Header
request.AddHeader("Accept", "application/json, text/json");
//Authentication add your token as an Authentication to your request object.
request.AddHeader("Authorization", "Token " + token);
//Optional, Trust the security certificate
//ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
//Send the Request
IRestResponse response = client.Execute(request);
// Optional: Exception handling
if (response.ErrorException != null)
{
const string message = "Error retrieving response. Check inner details for more info.";
var yourException = new ApplicationException(message, response.ErrorException);
throw yourException;
}
}
The object search functions are used to search, filter and find spider entities.
private void SearchExample(string token)
{
// The url of mandator you from the web api help page
var url = "{mandatorid}/search/Asset?Orderby=AssetNo&MaxRows=10&Firstrow=1";
// The Url of the Spider Web API
var yourApiUrl = "http://yourWebServer/SpiderWebApi";
// Create a Rest Client
RestClient client = new RestClient {BaseUrl = new Uri(yourApiUrl)};
// Initialize your Rest Request
RestRequest request = new RestRequest
{
Resource = url,
RequestFormat = DataFormat.Json
};
//Format Header
request.AddHeader("Accept", "application/json, text/json");
//Authentication add your token as an Authentication to your request object.
request.AddHeader("Authorization", "Token " + token);
//Optional, Trust the security certificate
//ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
//Send the Request
IRestResponse response = client.Execute(request);
// Optional: Exception handling
if (response.ErrorException != null)
{
const string message = "Error retrieving response. Check inner details for more info.";
var yourException = new ApplicationException(message, response.ErrorException);
throw yourException;
}
}
The report area enables the generation, filtering and retrieval of reports stored in Spider.
private void GetReportsExample(string token)
{
// The url of mandator you from the web api help page
var url = "{mandatorid}/report/{reportid}";
// The Url of the Spider Web API
var yourApiUrl = "http://yourWebServer/SpiderWebApi";
// the token you have from the first Token example, and you will use this token in every request
// Create a Rest Client
RestClient client = new RestClient {BaseUrl = new Uri(yourApiUrl)};
// Initialize your Rest Request
RestRequest request = new RestRequest
{
Resource = url,
RequestFormat = DataFormat.Json
};
//Format Header
request.AddHeader("Accept", "application/json, text/json");
//Authentication add your token as an Authentication to your request object.
request.AddHeader("Authorization", "Token " + token);
//Optional, Trust the security certificate
//ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
//Send the Request
IRestResponse response = client.Execute(request);
// Optional: Exception handling
if (response.ErrorException != null)
{
const string message = "Error retrieving response. Check inner details for more info.";
var yourException = new ApplicationException(message, response.ErrorException);
throw yourException;
}
}
The Spider Web API meta area provides information about the fields in search results and reports, as well as information about creating and editing entities.
In addition to the lists of available tenants, entities, searches, and reports, metadata contain additional information depending on the used function.
private MandatorObject GetMandatorsExample(string token)
{
// The url of mandator you from the Web API Help
var url = "meta/mandator";
// The Url of the Spider Web API
var yourApiUrl = "http://yourWebServer/SpiderWebApi";
// The token you have from the first token example, and you will use this token in each request
// Create a Rest Client
RestClient client = new RestClient {BaseUrl = new Uri(yourApiUrl)};
// Initialize Rest Request
RestRequest request = new RestRequest
{
Resource = url,
RequestFormat = DataFormat.Json
};
//Format the header
request.AddHeader("Accept", "application/json, text/json");
//Authentication: Add your token as an Authentication header to your request object.
request.AddHeader("Authorization", "Token " + token);
//Optional, trust the security certificate
//ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
//Send the Request
RestResponse response = client.Execute(request);
// Optional: Exception handling
if (response.ErrorException != null {
const string message = "Error retrieving response. Check inner details for more info.";
var yourException = new ApplicationException(message, response.ErrorException);
throw yourException;
}
//you can finally deserialize the result in your own object
// As an example the see below the MandatorObject definition
MandatorObject result = new MandatorObject();
if (response.StatusCode == HttpStatusCode.OK){
result = JsonConvert.DeserializeObject<MandatorObject>(response?.Content);
}
return result;
}
public class MandatorObject
{
public System.Data.DataTable Result { get; set; }
public string CultureCode { get; set; }
}
private void GetMetaSearchAssetExample(string token)
{
// The url of mandator you from the web api help page
var url = "{mandatorid}/meta/search/asset";
// The Url of the Spider Web API
var yourApiUrl = "http://yourWebServer/SpiderWebApi";
// Create a Rest Client
RestClient client = new RestClient {BaseUrl = new Uri(yourApiUrl)};
// Initialize your Rest Request
RestRequest request = new RestRequest
{
Resource = url,
RequestFormat = DataFormat.Json
};
//Format the header
request.AddHeader("Accept", "application/json, text/json");
//Authentication: Add your token as an Authentication header to your request object.
request.AddHeader("Authorization", "Token " + token);
//Optional, Trust the security certificate
//ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
//Send the Request
IRestResponse response = client.Execute(request);
// Optional: Exception handling
if (response.ErrorException != null){
const string message = "Error retrieving response. Check inner details for more info.";
var yourException = new ApplicationException(message, response.ErrorException);
throw yourException;
}
}
private void GetMetaReportExample(string token)
{
// The url of mandator you from the web api help page
var url = "{mandatorid}/meta/report/{reporid}";
// The Url of the Spider Web API
var yourApiUrl = "http://yourWebServer/SpiderWebApi";
// the token you have from the first Token example, and you will use this token in every request
// Create a Rest client
RestClient client = new RestClient {BaseUrl = new Uri(yourApiUrl)};
// Initialize your Rest Request
RestRequest request = new RestRequest
{
Resource = url,
RequestFormat = DataFormat.Json
};
//Format the header
request.AddHeader("Accept", "application/json, text/json");
//Authentication. add your token as an Authentication header to your request
request.AddHeader("Authorization", "Token " + token);
//Optional, Trust the security certificate
//ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
//Send the Request
IRestResponse response = client.Execute(request);
// Optional: Exception handling
if (response.ErrorException != null)
{
const string message = "Error retrieving response. Check inner details for more info.";
var yourException = new ApplicationException(message, response.ErrorException);
throw yourException;
}
}
private void GetMetaEntitiesExample(string token)
{
// The url of mandator you from the web api help page
var url = "{mandatorid}/meta/entities";
// The Url of the Spider Web API
var yourApiUrl = "http://yourWebServer/SpiderWebApi";
// Create a Rest Client
RestClient client = new RestClient {BaseUrl = new Uri(yourApiUrl)};
// Initialize your Rest Request
RestRequest request = new RestRequest
{
Resource = url,
RequestFormat = DataFormat.Json
};
//Format the Header
request.AddHeader("Accept", "application/json, text/json");
//Authentication add your token to your request object.
request.AddHeader("Authorization", "Token " + token);
//Optional, Trust the security certificate
//ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
//Send the Request
IRestResponse response = client.Execute(request);
// Optional: Exception handling
if (response.ErrorException != null)
{
const string message = "Error retrieving response. Check inner details for more info.";
var yourException = new ApplicationException(message, response.ErrorException);
throw yourException;
}
}
Jan 20, 2022 04:56 AM