<< Click to Display Table of Contents >> Navigation: REST API > EQuIS REST API Code Examples |
The following two examples demonstrate how a user can interact with the EQuIS Enterprise REST API using either JavaScript or C#.
The EQuIS REST API requires a REST API License applied to the EQuIS database (EQuIS Enterprise URL) and the EQuIS users must be assigned the REST API role. All operations require an authenticated user. Both code examples make an authenticated request, which require a token. Instructions for creating a token can be found in the User Profile Editor documentation.
This example is shown using JavaScript and makes an authenticated request to return a list of all facilities that the user has permission to view. Comments (denoted by // and green font color) describe what the code block will do.
//Create a HTTP request
var request = new XMLHttpRequest();
//Set the type of request (GET, POST, PUT, etc.)
var requestType = "GET";
//Set the site used to access the REST API
var requestSite = "https://{Enter your site address for accessing the REST API}";
//Set the REST API endpoint
var endpoint = "/api/facilities";
//Build the request
request.open(requestType, requestSite + endpoint);
//Add a user generated token for accessing the REST API
var token = {Enter your user generated token};
//Set the request header for authentication to access the REST API
var requestHeaderType = "Authorization";
//Set the type of token being used
var tokenType = "Bearer ";
//Build the request header that will be sent with the request
request.setRequestHeader(requestHeaderType, tokenType + token);
//Send the request
request.send();
//Display the results once the request has gotten a response
request.onload = function() {
if (request.status == 200) {
//Log the results if the request was successful
console.log(JSON.parse(request.response));
} else {
//Log the error type if the request was not successful
console.log("Error: " + JSON.parse(request.status));
}
}
This example is shown in C# and makes an authenticated request to drill into a specific facility and return the values for the requested facility. Comments (denoted by // and green font color) describe what the code block will do.
//Add necessary namespaces to avoid the need to qualify the use of that type in the namespace
using System;
using System.Net;
using System.IO;
public class Request
{
static void makeRequest()
{
//Set the site used to access the REST API
string requestSite = "https://{Enter your site address for accessing the REST API}";
//Set the REST API endpoint
string endpoint = "/api/facilities/{Enter a facility ID}";
//Create a HTTP request with the site used to access the REST API and the REST API endpoint
HttpWebRequest reportRequest = (HttpWebRequest)WebRequest.Create(requestSite + endpoint);
//Set the type of request (GET, POST, PUT, etc.)
reportRequest.Method = "GET";
//Set the request header for authentication to access the REST API
string requestHeaderType = "Authorization";
//Set the type of token being used
string tokenType = "Bearer ";
//Add a user generated token for accessing the REST API
string token = {Enter your user generated token};
//Build the request header that will be sent with the request
reportRequest.Headers.Add(requestHeaderType, tokenType + token);
//Get the results once the request has gotten a response
HttpWebResponse reportResponse = (HttpWebResponse)reportRequest.GetResponse();
//Create a stream from the request response
Stream streamResponse = reportResponse.GetResponseStream();
//Read from the stream
StreamReader readStream = new StreamReader(streamResponse);
//Build a char array for displaying the response
Char[] characters = new Char[256];
//Add a count to iterate over the stream
int count = readStream.Read(characters, 0, 256);
//Loop through the response and write response to the console
while (count > 0)
{
string content = new string(characters, 0, count);
Console.WriteLine(content);
count = readStream.Read(characters, 0, 256);
}
//Close the HttpWebResponse
reportResponse.Close();
//Close the StreamReader
readStream.Close();
}
public static void Main()
{
//Method calls the above code to make the request to the REST API
makeRequest();
}
}
Copyright © 2024 EarthSoft, Inc. • Modified: 04 Jan 2022