REST API Architecture and Standards
REST API architecture has been create to make the procedures to access data from different services easier and more standardized.
This logical architecture frequently uses json data structure to send and receive data. JSON is an open standard file format and data interchange format that uses human-readable text to store and transmit data objects consisting of attribute–value pairs and arrays.
Json data example:
{
"api": "v2.7",
"apipath": "https://www.rackcorp.net/api/v2.7"
}
This data could be represented as an array or object in many languages such as PHP, PYTHON and C#
Rackcorp has migrated many of its functionalities to work with REST API architecture to modernize and make easy the process to get data through HTTP protocol. Every month we are adding new services through REST API. It is important you follow us to get the last updates and the last version of our API.
Some important concept of REST API:
- HTTP methods - GET, POST, PUT, DELETE
Web developers are likely familiar with GET and POST, along with the other HTTP methods, also sometimes called HTTP verbs. These methods define the type of request being made to a REST API.
- Methods by use:
- GET - request data
- POST - insert new data
- PUT - update data
- DELETE - delete data
- Resource Names:
Resources are sometimes referred to as the nouns that the HTTP verbs act upon. Earlier web services were built around remote procedure calls, which saw APIs as extensions of the code that called them. By contrast, REST resources can be accessed with multiple HTTP methods.
-
GET /api/animals:
retrieve a list of animalsPOST /api/animals:
add a new animalGET /api/animals/dog:
retrieve a single animal by IDPUT /api/animals/dog:
update a single animal by IDDELETE /api/animals/dog:
delete an animal by ID
- Data Formats:
Most API requests will return content from the server that the client needs to interpret. Rarely is this content plain text—usually, it will use a structured data format. While REST does not specify any data formats, JSON and XML are the two most commonly used.
Json:
{
"id": "dog",
"name": "Pet dog",
"genus": "Canis",
"img": "https://cdn2.thedogapi.com/images/1MZ0YbOpS.jpg"
}
Xml:
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<id>dog</id>
<name>Domestic dog</name>
<genus>Canis</genus>
<img>https://cdn2.thedogapi.com/images/1MZ0YbOpS.jpg
</img>
</root>
- HTTP Statuses:
Since REST APIs depend upon HTTP standards, each request’s status is used to communicate the result of the request, such as success or failure. Each status code provides a machine-readable response, plus a human-readable message. Web developers (and a number of users) will be familiar with many of these.
-
- 200: Success
- 201: Created
- 401: Unauthorized
- 403: Forbidden
- 404: Not found
- 429: Too many requests
While REST is not a standard, there are many other standards often associated with REST. For example, OAuth covers third-party authorization for resources while JSON PATCH describes a standard approach to the HTTP PATCH method for the JSON data format. An important standard to keep in mind as you design your own APIs is the OpenAPI specification.
How to make a REST API request to get, insert and update data?
You can make a REST API using any programming language such as Javascript, PHP, PYTHON and C#. In this article we will add some samples of code to explain how to make a request using Javascript (JQUERY) and PHP. We choose these two languages because it is most popular language to deploy a website.
We strongly recommend only make REST API calls from your back-end code, be careful when use javascript code to call a REST API url. Never add your credentials in front-end code.
Javscript (JQUERY):
Firstly, you need to create a small logic to call the api url as the sample bellow:
<script>
let query = {};
const v = 'v2.7';
const URL = 'https://www.rackcorp.net/api/'+v+'/dcs';
$.ajax({
ur: URL,
type: 'GET',
data: query,
success: function(res) {
console.log(res);
alert(res);
},
error: function(res) {
console.log(res);
alert(res);
}
});
</script>
You can see that the code to make requests using REST API is quite simple.
We have a variable called 'data' which is the object which would contain the pair of key-values which will be translate into a query in back-end. Them, we create a string variable which is the URL to connect to the REST API HTTP. With this two variables, we are able to create the AJAX script to call the REST API. If the code is correct, the logic will redirect to SUCCESS, if the code is wrong it will redirect to ERROR. Them, you can treat the response ('rest') as you wish.
Be careful to use javascript to make REST API requests. It is because javascript is a front-end programming language, it means the code runs in the user desktop (notebook, phones) and this code can be intercepted by hackers and any other malicious person. REST API requests in javascript is only recommended when you dont need to pass sensitive data, like keys, password or personal information. In the sample above, we called an URL to list all Data Centers (DC) in Rackcorp which doesnt need authentication, it is open to the world.
PHP:
In PHP usually developers use CURL function to connect through HTTP. In the sample bellow you can understand how to implement a REST API request from your server to another server.
<?php
$v = 'v2.7';
$url = "https://api.rackcorp.net/api/". $v . "/dcs";
$query = [];
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($query));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
if($response) {
return json_decode($response, true);
}
return false;
As you can see we created the same variables 'v' and 'query' in PHP to call the REST API url. Them, we start creating the CURL initial command. We set some options in the CURL function such as POSTFIELDS and RETURNTRANSFER. You have many other options that you can add to CURL PHP. Have a look in the PHP docs to find out the best approach for your code logic.
Conclusion:
REST API request is a quite simple way to get, insert, and update data through internet with small effort. Nowadays, Mostly of services through the internet has implemented REST API functions and RACKCORP also has its own standard. If you need to connect to us using REST API, read more in RACKCORP REST API