What is the RESTful API?

Tram Ho

What is the RESTful API?

The importance of APIs in today’s applications is indisputable. An application without API is like a computer without internet connection. And as a matter of fact, everything that has evolved for a while will form common standards and for the API, it’s RESTful.

Although there are many articles about RESTful API nowadays, I still want to write about this issue to contribute a part in designing RESTful API. This article is also the knowledge I self-researched, referenced through many sources, if I have something wrong, please give me some more suggestions. Thanks you

Preface

It can be said that REST principles and RESTful data structures are widely known in web programming in general and application programming in particular.

It can be said that REST itself is not a technology. It is a method of creating API with certain organizational principles. These guidelines are intended to guide developers to create a comprehensive API request handling environment.

To better understand the RESTful API, we will take turns explaining the concepts of API, REST, or RESTful.

What is the RESTful API?

Web developers often mention REST principles and RESTFUL data structure because it is a very important part of the development of web applications. So what is the RESTFUL API? To better understand we learn together.

The RESTful API is a standard used in designing APIs for web applications (designing Web services) for ease of managing resources. It focuses on system resources (text files, images, audio, video, or dynamic data, etc.), including resource states that are formatted and transmitted over HTTP.

The components of it

API ( A pplication P rogramming I nterface) is a set of rules and mechanisms by which an application or component will interact with another application or component. The API can return the data you need for your application in common data types like JSON or XML.

REST ( RE presentational S tate ** T ** ransfer) is a type of data structure transformation, an architectural type for writing API. It uses a simple HTTP method to make communication between machines. So instead of using a URL for processing some user information, REST sends an HTTP request like GET, POST, DELETE, etc. to a URL to process the data.

The RESTful API is a standard used in designing APIs for web applications to manage resources. RESTful is one of the most commonly used API design types today to let different applications (web, mobile …) communicate with each other.

The most important function of REST is to specify how to use HTTP methods (such as GET, POST, PUT, DELETE …) and how to format URLs for web applications to manage resources. RESTful does not specify the application code logic and is not limited by the application programming language, any language or framework can be used to design a RESTful API.

How does the RESTful API work?

After we know what the RESTful API is, in this section we learn about its operating principle. Like other communication protocols or data structures. To understand the nature of the problem, first of all need to understand the principles of its operation.

REST works primarily on the HTTP protocol. The above basic operations will use its own HTTP methods.

  • GET (SELECT): Returns a Resource or a Resource List.
  • POST (CREATE): Create a new Resource.
  • PUT (UPDATE): Updated information for Resource.
  • DELETE (DELETE): Delete a Resource.

These methods or operations are often referred to as CRUDs, which correspond to Create, Read, Update, Delete – Create, Read, Edit, Delete.

Currently, most RESTful API developers now choose JSON as the official format, but many people choose XML as the format, generally free and fast to use.

Authentication request and return data structure

The RESTful API does not use sessions and cookies, it uses an access_token for each request. You can learn JWT (JsonWebToken) to know more. I will do another article about JWT in the next section ) The returned data usually has the following structure:

The above is an example of the return structure of an api get a list of users in the system.

Status code

When we request a certain API there will usually be some status codes to identify later:

  • 200 OK – Successful return for methods GET, PUT, PATCH or DELETE.
  • 201 Created – Returns when a Resource has been successfully created.
  • 204 No Content – Returns when Resource deleted successfully.
  • 304 Not Modified – Client may use cached data.
  • 400 Bad Request – Request is not valid
  • 401 Unauthorized – Request requires auth.
  • 403 Forbidden – denied permission.
  • 404 Not Found – Resource not found from URI
  • 405 Method Not Allowed – This method is not allowed on the current user.
  • 410 Gone – Resource no longer exists, older version no longer supports.
  • 415 Unsupported Media Type – Does not support this Resource type.
  • 422 Unprocessable Entity – The data is not authenticated
  • 429 Too Many Requests – Request was rejected due to restrictions

In Ruby on Rails you can use symbol status code or 3-digit integer

Manage the version of the api

When setting the api for the ios app or the client side, we should set the version for the api. For example, the following endpoint: api / v1 / users

This will help the system after upgrading to the new version still supports the APIs of the old version, as well as make maintenance and repair easier.

What are the advantages of RESTFUL API?

As shown above, the use of the RESTFUL API brings certain benefits to programmers. So what are the benefits it brings? Compared with other methods, it will have remarkable points

Some of the main advantages when using the RESTFUL API are:

  • Help make the application clearer
  • REST URLs represent resources, not actions
  • Data is returned in many different formats such as xml, html, json ….
  • The code is simple and concise
  • REST focuses on system resources

Websites today often use REST APIs to allow connections to their data. Facebook also provides REST APIs to help external applications connect to their data

Final

Thank you for reading this far, hope to help you in learning about RESTful API. In the next article, I will build a RESTful API app with Ruby on Rails, see you next time. Thanks again

Share the news now

Source : Viblo