This is the 9th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

preface

Web development mode has two, one is front end and separating two patterns before and after the end, end not separation mode before and after the need to write the HTML template file, while the front and back side after separation mode and focus on the backend interface, returned to the front end json or XML format data, in the actual project development, the separation of front end model with more, When developing with a separated front – and back-end pattern, you need to standardize the API as well as the interface specification. This article will introduce it

API interface

API interface refers to the URL link of the front and back information interaction rules through the network. It is the medium of communication between the front and back ends. The two sides agree on the format of data transmission. However, webAPI interface is different from general URL links. WebAPI interface has the following four characteristics:

  • 2. A URL link that looks like returned data:https://api.map.baidu.com/place/v2/search
  • The request mode can be GET, POST, PUT, Patch, or Delete
  • Request parameters: Key-value data in JSON or XML format
  • Response result: Data in JSON or XML format

Restful specification

REST stands for Representational State Transfer, which in Chinese means representation. RESTful is a design style that defines Web API interfaces, especially for applications where the front and back ends are separated. The philosophy of this style is that the back-end development task is to provide data, providing external access to data resources. So when defining the interface, the URL path that the client accesses represents the data resource to manipulate. In fact, we can implement restful apis using any of these frameworks.

Restful specifications include the following:

  • Data security: URL links are generally transmitted using THE HTTPS protocol, which improves data exchange security
  • Interface features, see the API words, it means that the request URL link is completed front and background data interaction:
- https://api.baidu.com/v1
- https://api.baidu.com/v2
Copy the code
  • Multiple versions coexist, and the data versions are identified in the URL link. V1 and v2 in the URL link are the embodiment of different data versions (only when one data resource has multiple versions).
- https://api.baidu.com/v1
- https://api.baidu.com/v2
Copy the code
  • Data is resources, interface in the url are use nouns, the interface is usually completed Taiwan before and after data interaction, interactive data we call resources, advocate commonly use the plural form of resources, try not to appear in the url links operating resources of verb, but special interface can be verb, because none of these interfaces are generally clear resources, Or the verb is the core meaning of the interface.
# Normal interface
- https://api.baidu.com/users
- https://api.baidu.com/books
-https://api.baidu.com/book # error model -https://api.baidu.com/delete-user # verb demonstration - https://api.baidu.com/place/search
 - https://api.baidu.com/login
Copy the code
  • Resource operations are determined by method. Resource operations generally involve adding, deleting, modifying, and querying resources. Request methods are provided to identify adding, deleting, modifying, and querying resources
-https://api.baidu.com/books - get request: get all the books-https://api.baidu.com/books/1 - get request: the primary key to 1 book-https://api.baidu.com/books - a post request: add a book book-https://api.baidu.com/books/1 - put request: modify the primary key to 1 book as a whole-https://api.baidu.com/books/1 - patch request: local modify primary key to 1 book-https://api.baidu.com/books/1 - the delete request: delete the primary key to 1 bookCopy the code
  • Filtering, passing search criteria in the form of url upload parameters
-Specifies the amount of records returned at https://api.example.com/v1/zoos?limit=10:-https://api.example.com/v1/zoos?offset=10: specified returns the starting position of record- https://api.example.com/v1/zoos?page=2&per_page = 100: specify what page, as well as the number of records per page - https://api.example.com/v1/zoos?sortby=name&order=asc: Specifies which attribute returns the result according to the sorting, and sort order - https://api.example.com/v1/zoos?animal_type_id=1: specifies a filter conditionCopy the code
  • Response status code. Different status codes represent different response status
  • Error handling: The error message error should be returned as the key of the error message
{error: "operation without permission "}Copy the code
  • Return results: For different operations, the server should return results to the user that conform to the following specifications
GET/Collection: returns a list (array) of resource objects. GET/Collection/Resource: returns a single resource object. POST/Collection: Returns a newly generated resource object. PATCH /collection/resource: Returns the full resource object DELETE /collection/resource: returns an empty documentCopy the code
  • A resource requiring a URL request requires a request link to access the resource
Hypermedia API, RESTful API best do Hypermedia, that is, return results to provide links to other API methods, so that users do not look up the document, but also know what to do next {"Status" : 0, "MSG" : "ok", "results" : [{" name ":" KFC restaurant (ROM) ", "img" : "https://image.baidu.com/kfc/001.png"}...]. }Copy the code

conclusion

The article was first published in the wechat public account Program Yuan Xiaozhuang, at the same time in nuggets.

The code word is not easy, reprint please explain the source, pass by the little friends of the lovely little finger point like and then go (╹▽╹)