07/07/2024
What is a REST API or RESTful API?
A RESTful API is an approach used to design APIs, or in other words, a type of architecture. In this approach, HTTP requests are used to access and manage data.
We already learned what an API is from a previous article, but to remind you again, an API, or Application Programming Interface, is a set of rules and protocols used for two software applications to communicate with each other. A RESTful API is one of the main architectural styles used to design these APIs.
A REST API is an API designed by following a software architectural style called Representational State Transfer. This architecture was introduced by Roy Fielding. REST APIs use HTTP requests (GET, POST, PUT, DELETE) to access and manage data. Through these requests, a resource, or data, is identified using a URL (Uniform Resource Locator).
One advantage of using REST APIs is scalability and flexibility. Even if a change is made to the API on the server-side, it does not cause any problem for the client-side. A REST API is completely stateless. In other words, when a server handles a request, that request is independent of previous requests. To explain this more clearly, you may know that when browsing the web, after accessing one page from a browser and then accessing another page, the server does not store any previous request history.
Things to know when designing a REST API
1. Resource Identification
In a REST API, a request is identified using a URL. Example: https://api.example.com/books/1. Here, books/1 refers to the book resource with the number 1.
2. Use of HTTP Methods
There are four main HTTP methods:
GET – Used to read a resource
POST – Used to create a new resource
PUT – Used to update an existing resource
DELETE – Used to delete a resource
3. Stateless Interactions
A REST API is stateless. That means when a server processes a request, it does not store any information about previous requests.
4. Use of JSON/XML
JSON (JavaScript Object Notation) and XML (eXtensible Markup Language) are used for data exchange. JSON is easier to use and lightweight.
To learn more things like this, Like this post and Follow our Page.
Sinhala Translation
REST API එහෙමත් නැත්තම් RESTful API කියන්නෙ මොකද්ද?
RESTful API එකක් කියන්නෙ API design කරන්න use කරන approach එකක්, එහෙමත් නැත්තම් architectural type එකක්. මේකෙදි වෙන්නෙ HTTP requests භාවිතා කරලා data access කිරීම සහ manage කිරීම.
API එකක් කියන්නෙ මොකද්ද කියලා අපි කලින් article එකෙන් දැනගත්තනේ, ආයෙත් මතක් කරොත් API එකක්, එහෙමත් නැත්තම් Application Programming Interface එකක් කියන්නෙ software applications දෙකක් එකිනෙක සමග communicate කිරීම සදහා යොදාගන්න rules ටිකක් සහ protocols ටිකක්. RESTful API කියන්නෙ මේ APIs design කරන main architectural type එකක්.
REST API එකක් කියන්නෙ Representational State Transfer කියන software architectural style එකක් follow කරලා design කරන API එකක්. මේ architecture එක හඳුන්වලා දීලා තියෙන්නෙ Roy Fielding විසින්. REST APIs වලින් data සහ access සහ manage කිරීම සදහා HTTP requests (GET, POST, PUT, DELETE) භාවිතා කරනවා. මේ requests වලින් resource එකක්, එහෙමත් නැත්තම් data එකක් identify කරන්නේ URL (Uniform Resource Locator) එකකින්.
REST APIs භාවිතා කරන advantage එකක් තමයි scalability සහ flexibility. API එකක්, server-side එකකදී change එකක් කරා කියලා client-side එකට කිසිම problem එකක් වෙන්නෙ නෑ. REST API එකක් සම්පූර්ණයෙන්ම stateless. එහෙමත් නැත්තම්, server එකක් request එකක් handle කරද්දි, ඒ request එක previous requests වලින් independent වෙනවා. මේ ගැන තව පැහැදිලි කරොත්, ඔයාලා දන්නවා ඇති, web browsing කරනකොට, browser එකකින් එක page එකකට access කරාට පස්සේ වෙනත් page එකකට access කලා කියලා server එකේ කිසිම previous request history එකක් save වෙන්නෙ නෑ.
REST API design කරනකොට දැනගත යුතු කාරණා
1. Resource Identification
REST API එකක request එකක් identify කරන්නේ URL එකකින්. Example: https://api.example.com/books/1. මෙහිදී books/1 කියන්නෙ, 1 number book resource එක.
2. Use of HTTP Methods
HTTP methods ප්රධාන 4 ක් තියෙනවා,
GET - Resource එකක් read කිරීම සදහා
POST - නව resource එකක් create කිරීම සදහා
PUT - පවතින resource එකක් update කිරීම සදහා
DELETE - Resource එකක් delete කිරීම සදහා
3. Stateless Interactions
REST API එකක් stateless. ඒ කියන්නෙ, server එකක් request එකක් process කරනකොට, previous requests ගැන කිසිම තොරතුරක් store කරගන්නෙ නැති එක.
4. Use of JSON/XML
Data exchange කිරීම සදහා JSON (JavaScript Object Notation) සහ XML (eXtensible Markup Language) භාවිතා වෙනවා. JSON භාවිතා කිරීම සදහා ලේසි සහ lightweight.
තවත් මේ වගේ දේවල් දැනගන්න මේ Post එකට Like කරලා අපේ Page එක Follow කරන්න SerendibTech 😎