Introduction
In architectural design, there are two main viewpoints. The first is starting with a blank slate and constructing an architecture out of standard components until it meets the system's requirements. The second is starting with the system's demands, with no limitations, and then incrementally identifying and applying constraints to system elements, allowing the forces that impact system behaviour to flow naturally.
The first promotes unconstrained vision and creativity, while the second emphasises the system environment's restraint and knowledge. The latter method was used to create REST.
REST is an acronym for REpresentational State Transfer, and this is an architectural style for distributed hypermedia systems. A RESTFul API is an application programming interface that adheres to the REST architectural style's constraints and allows interaction with RESTful web services.
RESTFul APIs allow you to design any online application that supports all CRUD (create, retrieve, update, and delete) actions. In this article, we'll be seeing how to handle requests in RESTFul APIs.
Get Request
GET queries are used to get resource representations or information only, not to modify it. GET requests are believed to be safe because they do not modify the state of the resource.
Furthermore, GET APIs should be idempotent, which implies that repeating identical searches should always return the same response unless another API (POST or PUT) alters the status of the server resource.
If the data-producing process is being referred to by Request-URI, the produced data, not the process's source text, should be returned as the entity in the response unless that text is the process's output.
- If the resource is on the server, the HTTP GET API must return HTTP response code 200 (OK) and the response body, commonly XML or JSON content (due to their platform-independent nature).
- If the resource is not on the server, it must return the HTTP response code 404 (NOT FOUND).
- Similarly, if the server determines that the GET request is not correctly structured, it will return HTTP response code 400. (BAD REQUEST).
For example:
app.get("/", function(req,res){
res.render("home");
});