API It refers to some functions encapsulated by an application , It is provided to other applications or developers . adopt API, You can easily use the functions of this application , There is no need to know the internal source code of this application .
Web API yes API One of them , Its function is similar to that of API It's the same , But what it provides to the outside world is some url Rules, not functions , Including the following 4 Parts of :
url:url link ;
Request mode :get、post、patch、delete etc. ;
Request parameters :json or xml Format key-value Type data ;
In response to the results :json or xml Format key-value Type data .
REST yes REpresentational State Transfer( Declarative state transitions ) An acronym for . It is the architecture style of distributed hypermedia system , By the first Roy Fielding stay 2000 In his paper in .
REST-ful, among ful For adjectives , Such as helpful、powerful. This kind of adjective means "full of,having the quality of". Add more after a noun to indicate “ Full … Of 、 Easy to …、 can … Of 、 rich … Of 、 have … Of ” It means , Is the most commonly used adjective suffix , The antonym suffix is -less.RESTful It means satisfaction REST principled
Reference from :RESTful API
REST A set of architectural constraints and principles , Usually used for Web Service development . It does not propose a specific implementation , Just put forward some guidelines , For our reference during development . We can follow its specifications , You can also ignore ( Don't suggest ).
If an architecture conforms to REST The constraints and principles of , We call it RESTful framework .
Safety guarantee
For safety's sake , You should use https agreement .
use api Keyword identification api url, And ordinary url To differentiate . Such as :
www.xyz.com/api/xxx/,api.xyz.com/xxx/
stay url Add version version to , Or put the version information in the request header , Request different versions of the same resource . Such as :
api.xyz.com/v1/xxx,Accept: application/vnd.xyz+json;version=1.0.
Data is a resource , Nouns should be used ( In the plural ). Such as :www.xyz.com/book/.
HTTP Verb , Request method (method)
The operation on the resource is determined by the request mode !
Idempotency : The same REST Multiple access to the interface , The resource status is the same .
Security : For the REST Interface access , It will not change the state of the server-side resources .
By means of url Submit filter conditions in the form of passing parameters on . Such as :
https://api.example.com/v1/zoos?limit=10: Specify the number of returned records
https://api.example.com/v1/zoos?offset=10: Specify where to start the return record
https://api.example.com/v1/zoos?page=2&per_page=100: Specify page , And the number of records per page
https://api.example.com/v1/zoos?sortby=name&order=asc: Specifies which attribute to sort the returned results by , And sort order
https://api.example.com/v1/zoos?animal_type_id=1: Specify filter criteria
HTTP Status code
1xx( Information ): Communication transmission protocol level information
1XX A series of response codes are only used in conjunction with HTTP Use when communicating with the server , It is seldom used .
2xx( success ): Indicates that the client's request has been successfully accepted
200(OK): Express REST API Any operation requested by the client was successfully performed ;
201( Created): The user successfully created a new resource .
202(Accpted): The request has been accepted and added to the processing queue , But the processing has not been completed .
204(No Content): Server processed successfully , But there is nothing to return . Commonly used in PUT、POST perhaps DELETE Response to the request .
204 The response must never contain a message body , Therefore, it always terminates at the first blank line after the header field .
3xx( Redirect ): Indicates that the client must perform some other operation to complete its request
301(Moved Permanently): Requested URL Has been permanently removed , And designed a new URL, The client should use the new URL.
REST API Should be in response to Location Specify a new... In the header URL, And put the old URL Requests are directed to new URL.
302(Found): And 301 similar , But resources are only temporarily moved , The client should continue to use the original URL.
302 Is to perform URL Common ways of redirection .
304(Not Modified): If the client is sending GET Attach with request if-Modified-Since Headlines , And the resource has never been modified since the specified version of the header , It means that the cache resources of the client are up to date , Require clients to use caching to save resources .
This status code is similar to 204, Response body must be empty .
4xx( Client error ): This error status code points to the client
400(Bad Request): This is a general client error status , Indicates the syntax error of the client request , Server does not understand .
401(Unauthorized): The client attempted to operate on a protected resource , But did not provide the correct certificate ( token 、 user name 、 Wrong password ).
The response must contain WWW-Authenticate Header fields , This includes which authentication the appropriate server will accept .
403(Forbidden): And 401 Error relative , Indicates that the client's request is correct , But the user does not have the necessary permissions for the resource .
This response code is often used for a resource that is only allowed to be accessed for a specific period of time , Or allow specific IP User access to the address .
404(Not Found): The server could not find the resource at the request of the client ( Webpage ).
405(Method Not Allowd): The client attempted to use a resource that is not allowed HTTP Method . such as , One resource only supports GET Method , But the client uses PUT Method access .
405 The response must contain Allow header , This header lists the supported by the resource HTTP Method .
406(Not Acceptable): Format requested by user is not available . Such as user request JSON Format , But only XML Format .
5xx( Server error ): The server is responsible for these error status codes
500(Internal Server Error): This is a generic server response . For most web frame , If an exception is encountered while executing the request processing code , They send this response code .