REST and RESTful APIs

REST

What is REST

REST is the World Wide Web software architectural style

REST is a design style and development method for web applications. Based on HTTP, it can be defined in XML format or JSON format .

REST is suitable for scenarios where mobile Internet manufacturers serve as business interfaces, realizing the function of third-party OTT calling mobile network resources, and the action types are adding, changing, and deleting the called resources.

Introduction to REST

REST:Representational State Transfer

  • Representational: data representation
  • State: current state or data
  • Transfer: data transfer

For more exciting content, please search for " " on WeChat前端爱好者 , and click me to view .

REST six limitations

Client-Server (Client/Server)

  • separation of concerns
  • The server focuses on data storage, improving simplicity (making the server code simpler)
  • The front end focuses on the user interface, which improves portability (easy to port to other projects)

Stateless

  • All user session information is kept on the client side
  • Each request must contain all information
  • Cannot rely on contextual information
  • The server does not need to save session information
  • Simplicity, Reliability, Visibility

Cache

  • All server responses must be marked as cacheable or not cacheable
  • Reduce front-end and back-end interactions and improve performance

Uniform Interface

  • The interface design is as unified and universal as possible
  • The interface is decoupled, and the front and back ends are independently developed

Layered System

  • Each layer only knows about the adjacent layer
  • The client does not know whether to communicate with the proxy or the real server
  • Other layers: security layer, load balancing, caching layer

Code-On-Demand

  • The client can download and run the code from the server
  • Simplify the client by reducing some functionality

Unified Interface Limitations

The restriction of the unified interface is a sub-restriction of the "unified interface" in the 6 REST restrictions introduced earlier, which is to tell us what the REST-style interface should be designed to be.

Resource ID

A resource is any transaction that can be named

  • Each resource can be uniquely identified by a URI

    • https://api.github.com/users
    • https://api.github.com/users/123

Manipulating resources through representations

Expression is Representation

  • The client cannot directly manipulate server resources
  • Clients should manipulate resources through representations – JSON

self-describing information

Each message must provide enough information for the recipient to understand

  • Media type (application/json)
  • HTTP method: GET, POST
  • Whether to cache: Cache-Control

Hypermedia as an Application State Engine

  • Hypermedia: Links with text
  • Application state: a web page
  • Engine: drive, jump

Click a link to jump to another page

RESTful API

RESTful API is a REST-style API, that is, rest is an architectural style that has nothing to do with programming languages ​​or platforms, and uses HTTP as the transmission protocol.

Introduction to RESTful APIs

What exactly does a RESTful API look like?

  • Basic URI like https://api.github.com/users
  • Standard HTTP methods such as GET, POST, PUT, DELETE
  • The data media type transmitted , such as JSON, XML

example

  • GET /users Get user list
  • GET /users/12 View a specific user
  • PUT /users/12 update user 12
  • DELETE /users/12 delete user 12

Best Practices for RESTful API Design

request design specification

  • URI uses nouns, try to use plurals , such as /users
  • URIs use nesting to represent associations, such as /users/12/repos/5
  • Use the correct HTTP method , such as GET/POST/PUT
  • Cases that do not conform to CRUD: POST/action/subresource

Responsive design specification

Corresponding: the return value of the interface

  • Inquire
  • paging
  • field filter
  • status code
  • error handling

safety regulations

  • HTTPS
  • authentication
  • Limiting

developer friendly

  • document
  • hypermedia

おすすめ

転載: blog.csdn.net/BradenHan/article/details/130838673