REST API & HTTP
REST API which stands for REpresentational State Transfer is an architectural style for distributed systems. But before deep diving into REST API let’s see what an API actually is; API (Application Programming Interface), is a set of rules that allows programs to communicate and share resources with each other. So now in simple language, REST is a set of rules and architectural patterns that developers follow to create APIs. Those APIs which are created in the server are accessed using Uniform Resource Locators (URL) by the client interface bound by HTTP (Hyper Text Transfer Protocol).
HTTP is a protocol that allows any data exchange and fetching of resources between client and server.
Each request initiated by the client has some actions to perform, and the server sends back a response corresponding to those actions. There are few things we need to know about HTTP during this request-response cycle and they are
- HTTP methods
- HTTP request
- HTTP response
- HTTP status codes
HTTP methods
HTTP defines a set of rules or request methods to indicate the desired action to be performed for a given resource. These methods check the information provided by the client, like what tasks to execute, if the instruction is valid or not, will the server respond, etc. Each request implements a different semantic but shares some common features by a group of them: eg. a request can be safe, idempotent, or cacheable. There are various types of HTTP methods that correspond to various actions. Most common and methods corresponding to CRUD operations are as follows:
GET: The GET method requests correspond to read action. Requests using GET should only retrieve data.
POST: The POST method is used to submit an entity to the specified resource, often causing a change in state or side effects on the server. This method requests correspond to create action.
PUT: The PUT method replaces all current representations of the target resources with the request payload and corresponds to update action.
DELETE: The DELETE method deletes the specified resource.
HTTP request
The HTTP request is a packet of information sent by a client to the server to retrieve some information. For instance, if we want to send an application form to any institution via web the form is the information that HTTP will decide which server to send and which server will receive it. Major components of the HTTP request are request URL, HTTP method, Status code, request header, request payload as shown in the image below.
HTTP response
After receiving and interpreting an HTTP request, a server responds with an HTTP response message. HTTP response message also consists of various parts like the resource returned by the server, status code of how the connection went by, and response headers.
HTTP Status Codes
HTTP specifies a set of codes to provide a clear message to users what is happening with their information/instruction. Each status code range is described below, including a description of which method(s) it can follow and any metainformation required in the response.
- Range 2XX: This class of status code indicates that the client’s request was successfully received, understood, and accepted.
- Range 3XX: This class of status code indicates that further action needs to be taken by the client in order to fulfill the request; i.e client is redirected to a different resource
- Range 4XX: This class of status code is intended for cases in which the error that originates from the client has occurred. These status codes are applicable to any request method.
- Range 5XX: This class of status code indicates cases in which the server is aware that it has an error or is incapable of performing the request.