Home  >  Article  >  Web Front-end  >  These core knowledge points must be mastered in front-end and back-end communication!

These core knowledge points must be mastered in front-end and back-end communication!

藏色散人
藏色散人forward
2022-08-07 13:47:531568browse

What is restful?

restful is the most popular interface design specification at the moment, a specification that both front-end and back-end must know
This specification defines each process in interface development

Protocol

The client should use the HTTPS protocol when communicating with the back-end service through the API

Domain name

API should Deployment under a dedicated domain name should be kept as simple as possible. If you are sure that the API is very simple and will not be further expanded, you can consider placing it under the main domain name. Here are two common URL root examples:

  • api.example.com/* A single subdomain name Used to access the interface
  • example.com/api/* A separate space under the main domain name

Versioning

All The API must remain backward compatible. You must introduce a new version of the API while ensuring that the old version of the API is still available. Therefore, version support should be provided for it

Address (path) routing (router)

The specific URL of the API

  • The naming of the URL must be All lowercase
  • The name of the resource in the URL must be a noun and must be in the plural form
  • The Restful type of URL must be used first
  • URL must be easy to read
  • URL must not expose the server architecture

As for whether the URL must use hyphens (-) or underscores (_), there is no hard and fast rule, but it must be unified according to the team situation. Style

  • https://api.example.com/zoos
  • https://api.example.com/animals
  • https://api .example.com/zoos/{zoo}/animals
  • https://api.example.com/animal_types
  • https://api.example.com/employees

Verb (request method method)

The specific operation type of the resource is represented by the HTTP verb. There are five commonly used HTTP verbs (the corresponding SQL commands are in parentheses).

  • GET (SELECT): Get the resource (one or more items) from the server.
  • POST (CREATE): Create a new resource on the server.
  • PUT (UPDATE): Update resources on the server (the client provides the complete changed resources) and fully update.
  • PATCH (UPDATE): Update in the server update resource (client provides changed attributes) part.
  • DELETE (DELETE): Delete resources from the server.

url parameters

All URL parameters must be in all lowercase, and the underscore type parameter form must be used
The paging parameters must be fixed to page, page_size

Response

The appropriate HTTP status code must be selected.
Must return JSON format data.
All errors must be in the same error format.

Common status codes

  • 1xx means that the request has been accepted and needs to continue processing. Generally,
  • 2xx does not appear in the API. The request has been successful. , the response header or data body expected by the request will be returned with this response
    • 200 The server successfully returns the data requested by the user
    • 201 The user successfully creates or modifies the data
    • 202 means A request has been queued in the background (no return value if successful, it is asynchronous)
    • 204 Deletion successful
  • 3xx Redirect
  • 4xx Client Reasons for the error
    • 404 The requested interface does not exist, the error type (the request address is wrong)
    • 401 The user is not logged in
    • 403 The user is logged in, but does not have permission to access
    • 422 Client parameter error, verification error
    • 429 Request limit exceeded
  • 5xx Error caused by server reasons
    • 500 The server encountered an unexpected situation, which caused it to be unable to complete the processing of the request. The server code reported an error
    • 503 Server is under maintenance
    • 504 Timeout call

Error format

'message' => ':message',          // 错误的具体描述
'errors' => ':errors',            // 参数的具体错误描述,422 等状态提供
'code' => ':code',                // 业务自定义的异常码
'status_code' => ':status_code',  // http状态码
'debug' => ':debug',              // debug 信息,非生产环境提供

What is a message?

HTTP messages are used to exchange information when HTTP requests and responses. Messages flow from the client to the server.

The messages are divided into two categories: request messages and response messages.

The basic message structures of request and response messages are the same, and they are both composed of Composed of three parts:

  • Starting line: Describe the message
  • Header: Some attributes
  • Subject: Data[optional]

axios

Compatible with browsers and node servers
Create XMLHttpRequests from the browser
Create http requests from node.js

Support Promise API
Support interception of requests and responses
Convert request data and response data
Cancel request
Automatically convert JSON data
Client supports defense against XSRF

Come on, stranger People! (I am no stranger to you here and there, I hope it will be helpful to you)

The above is the detailed content of These core knowledge points must be mastered in front-end and back-end communication!. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete