Intro of API, REST, and RESTful API

REST & RESTFUL API are often asked in my interviews recently, so I decided to briefly record it.

What is API?

API(Application Programming Interface) is a way for applications, systems or components to communicate with each other.

What is REST?

  • REST is the abbreviation of Representational State Transfer. It is a software architecture and a design style for CRUD between the server and the client.
    • CRUD represents CREATE/READ/UPDATE/DELETE
  • Some REST principles
    • Unified interface: Abstract the details of operations and provide unified operation methods and specifications.
    • Stateless: Stateless means that the server is independent of all previous requests, so the client can request resources in any order.
    • Layered system: The client does not know how many layers the server has, and the server can even request resources from other servers.
    • Cacheability: The client caches some information after getting the first response, and then directly uses the cache to obtain information later. (For example: header, footer, LOGO, etc. of each page in the website)
    • Coding on demand: 1. Code on demand: Server can expand its functions at any time to meet the immediate needs of Client.

RESTful API

  • An API that conforms to the REST design style is called RESTful API
  • Advantages include :
    • Scalability
      • Because REST optimizes client-server interaction, systems implementing REST APIs scale efficiently.
      • Statelessness eliminates server load since the server does not have to retain information about past client requests. Well-managed caches partially or completely eliminate some client-server interactions. All of these features support scalability without causing communication bottlenecks that reduce performance.
    • Flexibility
      • RESTful web services support complete client-server separation.
      • Platform or technology changes to server applications will not affect client applications.
      • Layered application functionality further increases flexibility. For example, developers can make changes to the database layer without rewriting application logic.
    • Independence
      • REST API is independent of the technology used
        • Client and server applications can be written in a variety of programming languages ​​without affecting API design.
        • You can also change the underlying technology on either end without affecting communication

Implement the RESTful API

  • Unique resource identifier
    The server uses a unique resource identifier to identify each resource.
    • For REST services, servers typically use Uniform Resource Locators (URLs) to perform resource identification.
    • URL specifies the path to the resource. A URL is similar to the website address you enter into your browser to view any web page. A URL, also called a request endpoint, clearly specifies to the server what the client needs.
  • Common Http methods include
    • GET
    • POST
    • PUT
    • DELETE
    • PATCH
      • differences between POST and PATCH
        • PATCH : Partial update to a resource
        • POST : Replace the entire resource with a new representation
  • HTTP header: Relay data between Client and Server, including required parameters (path parameters, Cookie parameters, etc.)

Examples

index | /comments | GET | all information

new | /comments | GET | Form to display new information

create | /comments/new | POST | New information

show | /comments/:id | GET | Show information

edit | /comments/:id/edit | GET | show information

update | /comments/:id | PATCH | Edit information

delete | /comments/:id/edit | DELETE | Edit information

Reference

Representational State Transfer (REST)
Representational State Transfer (REST) is a set of architectural principles used to describe how networked resources are defined and addressed.
什麼是 RESTful API?|ExplainThis
API 全名為 Application Programming Interface,最簡單的理解為,我們不需要知道他實際上是如何實作的,只要知道要怎麼使用它即可。舉例來說:就像你走進一間餐廳,在菜單上畫好品項後遞給老闆,老闆就能夠提供你需要的餐點,而你不需要去在意餐點是怎麼被實做出來的。所以比起在意它實際上怎麼被製作出來的,我們更在意怎麼獲得想要的東西,因此會更在意:「輸入的方法」以及「輸出的結果」,對應上面的例子就是:「該如何點餐」以及「餐點的結果」。
什麼是 RESTful API?– RESTful API 介紹 – AWS
了解什麼是 RESTful API、企業使用 RESTful API 的方式和原因,以及如何將 API Gateway 與 AWS 結合使用。
What are the benefits and drawbacks of using PATCH for updating resources?
Learn what HTTP methods and RESTful APIs are, and compare the benefits and drawbacks of using PATCH vs PUT for updating resources on a server.