Backend Basics: RESTful API (API, REST, Methods, JSON, Examples)
As you probably know, a typical modern application consists of 2 main parts: Frontend and Backend.
If you want to create a web or mobile app, you need to have at least a basic understanding of REST API which is commonly used on backend development.
Todays technology enables developers to create complex backend apps using these simple concepts in no time by using BaaS platforms like Altogic.
RESTful API
Let’s break it down and understand the REST & API terms.
Italian Restaurant Metaphor
Let’s say you are trying to find nearby Italian restaurants on your favorite restaurant review app. You open up your mobile app, type “Italian” into a search field, hit enter, and you see a list of restaurants near your location.
A REST API works similarly. You request something, and you get a list of results back from the service you are requesting.
What is API?
An API is an Application Programming Interface. It is a set of rules that allow applications to talk to each other. The developer creates the API on the server and allows the client to talk to it.
In our Italian restaurant example, the server is the application running in the cloud, and the client is the mobile app you use to search for Italian restaurants.
What is REST?
REST determines what the API looks like. It stands for “Representational State Transfer,” and it is a set of rules that developers follow when they create their API. In the above example, the search functionality is an API resource exposed by REST.
Methods
In RESTful API, the client and server communicate using the HTTP protocol. The client sends a request to the server, and the server processes this request and sends back a response in return.
Here is the HTTP methods that are commonly used in RESTful APIs:
1. GET
This request is used to get a resource from a server. If you perform a GET request, the server looks for the data you requested and sends it back to you. In other words, a GET request performs a READ operation. This is the default request method.
2. POST
This request is used to create a new resource or perform an action on a server. If you perform a POST request, the server creates a new entry in the database or performs an action and tells you whether the creation is successful or returns the action results according to your service design.
3. PUT/PATCH
Primarily used for updating a resource on a server. For example, if you perform a PUT/PATCH request, the server updates an entry in the database and tells you whether the update is successful. In other words, a PUT/PATCH request performs an UPDATE operation.
4. DELETE
This request is used to delete a resource from a server. If you perform a DELETE request, the server deletes an entry in the database and tells you whether the deletion is successful. In other words, a DELETE request performs a DELETE operation.
Okay, I got it. Can you show me an example usage of RESTful API in an app?
As I told you, todays technology enables developers to create complex backend apps with these simple concepts in no time by using BaaS platforms.
Let’s see an example with Altogic where we use RESTful API to create our backend app:
1. Create your database models
2. Define your business logic (services)
3. Create your RESTful Endpoints and associate them with your services
In this case, when a POST request is sent to the path “/posts/{postId}/comments” (3rd photo), it will trigger the “Add comment to a post service” (2nd photo) which creates a comment object under “comments” model on our database (1st photo) with the input given.
This is the logic behind your backend apps. You can create complex backend apps and connect them to your frontend using Altogic client library with ease.
What is JSON?
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write.
It is mainly used to store data in document databases and transport data between different applications, primarily between a server and web applications.
JSON is also frequently used in RESTFul APIs to provide data in request and response body. Below is an example JSON document summarizing a menu of a restaurant.
JSON supports the following two data structures;
- Collection of name/value pairs
- An ordered list of values − It includes arrays, lists, or sequences, etc.
A name/value pair consists of a field name (in double quotes), followed by a colon, followed by a value. So in the below example, the name is “firstName”, and the value is “John”.
Example: “firstName”: “John”
You need to put text values between double-quotes. For numbers, boolean values, or null values, you do not need double quotes.
Example: “age”: 24
In JSON, values must be one of the following data types:
- String — Strings in JSON must be written in double quotes
- Number — Numbers in JSON must be an integer or a floating-point
- Object (JSON object) — Values in JSON can be objects. However, objects as values in JSON must follow the same rules as JSON objects.
- Array — Values in JSON can be arrays. Array values are provided between []
- Boolean — Either true or false value
- Null — Empty value or no value
Example usage in Altogic Tester
You can send a POST request to the path “/posts” to trigger the “Create posts object” service which will create a posts object under posts model on the database with the input sent in JSON format:
Thanks for reading! Follow Altogic for more content like this, sample full-stack apps with full resources and tutorials.