Skip to main content

Start RESTful API

REST (Representational State Transfer)

badges_REST_API_Express_Stage1.png

What is RESTful API?

RESTful API is an interface design based on REST architecture that enable two computer systems exchange information securely over the internet either used to fetch or give some information from a web service. By REST architecture has imposes conditions on how an API should work All communication done via REST API uses only HTTP request.

Example: To generate monthly pay slips, your internal accounts system has to share data with your customer's banking system to automate invoicing and communicate with an internal timesheet application. RESTful APIs support this information exchange because they follow secure, reliable, and efficient software communication standards.


How do RESTful APIs work?

rest_api_works.png

The basic function of a RESTful API is the same as browsing the internet. The client contacts the server by using the API when it requires a resource. These are the general steps for any REST API call:

  1. The client sends a request to the server by format the request in a way that the server understands.
  2. The server authenticates the client and confirms that the client has the right to make that request.
  3. The server receives the request and processes it internally.
  4. The server returns a response to the client. The response contains request status and information that client requested.

The REST API request and response details vary slightly depending on how the API developers design the API.

Working: A request is sent from client to server in the form of a web URL as HTTP GET or POST or PUT or DELETE request. After that, a response comes back from the server in the form of a resource which can be anything like HTML, XML, Image, or JSON. But now JSON is the most popular format being used in Web Services. 


HTTP (Hypertext Transfer Protocol)

HTTP is a protocol for fetching resources such as HTML documents. It is the foundation of any data exchange on the Web, and it is a client-server protocol. In HTTP there are four methods that are commonly used in a REST-based Architecture i.e., POST, GET, PUT, and DELETE. These correspond to create, read, update, and delete or CRUD operations respectively.

http-2048x1107.jpg

HTTP request

GET: The HTTP GET method is used to read (or retrieve) a representation of a resource.

  • HTTP response code of 200 (OK). 
  • HTTP response error case, it most often returns a 404 (NOT FOUND) or 400 (BAD REQUEST).

POST: The POST verb is most often utilized to create new resources. In particular, it’s used to create subordinate resources.

  • HTTP response code of 201 (On successful creation) and a Location header with a link to the newly created resource with the 201 HTTP status. 

PUT: It is used for updating the capabilities. However, PUT can also be used to create a resource in the case where PUT is go to a URI that contains the value of a non-existent resource ID. 

  • HTTP response code of 200 (OK) or 204 if not returning any content in the body from a PUT.
  • If using PUT for create HTTP response code of 201 (On successful creation) same as POST

DELETE: It is used to delete a resource identified by a URI.

  • HTTP response code of 200 (OK) along with a response body.

Example of HTTP with params

what-are-url-parameters12172022.jpg

When the HTTP method is used to request certain resources from the web server, the client also sends some parameters to the web server. These parameters are in a pair of names and their corresponding values after "?" (query string begins) which are called HTTP parameters.