In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Today, I will talk to you about how to understand Restful API design according to Douban api, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.
1. What is REST?
The full name of REST is Representational State Transfer, which expresses the meaning of state transfer. It was first proposed in Roy Fielding's doctoral thesis. REST itself does not create new technologies, components, or services. Its idea is to make better use of existing web specifications on top of existing technologies. The web server with REST specification can better show the resources, and the client can make better use of the resources. Each resource is identified by URI/ID. REST itself has nothing to do with http, but currently http is the only instance related to it. REST has the characteristics of elegance and simplicity. This article is based on Douban api to talk about some of my understanding of restful.
2.URI specification
URI (Uniform Resource Identifiers) uniform resource identifier
URL (Uniform Resource Locator) uniform resource locator
The format of URI:
The format of URI is defined as follows: URI = scheme ": / /" authority "/" path ["?" Query] ["#" fragment]
Uri represents a resource that needs to be elegant and concise.
It is best to indicate the version at the api address.
such as
Https://api.douban.com/v2
About the delimiter "/", such as:
The "/" separator is generally used to classify the resource hierarchy. For example, https://api.douban.com/v2/book/1220562 describes the book number 1220562 under the book warehouse under Douban api,version2.
URI tries to use "-" instead of the underscore "_".
URI uniformly uses lowercase letters
URI does not contain file extension
Use? Used to filter resources, such as? Limit=10: specifies that 10 records are returned.
Don't use meaningless strings and numbers, be concise.
3. Correct use of method
Get-only for reading resources.
Post- is used to create a new resource.
Delete- is used as a resource for deletion.
Put-by using it as an update resource or create a resource
Head- only gets the header information of a resource.
For example, Douban Book api:
Namemethodapi acquires book information get/v2/book/:id user collects a book post/v2/book/:id/collection user modifies a book's collection put/v2/book/:id/collection user deletes a book's collection delete/v2/book/:id/collection
In addition, use post in some cases that are not curd compliant.
Convert actions into resources
For example, in the above interface, the exposed interface for a user's collection of a book is "/ v2/book/:id/collection", and the collection action is displayed through the post method, rather than directly writing in api, the collection "collection", ranking, and action are directly converted into resources.
4. Select the appropriate status code
The http request needs to return a status code, and the established status code can help the development team improve communication efficiency.
2xx: request is processed normally and returned
3xx: redirect
4xx: the client request has an error
5xx: the server request has an error
For example, the status code returned by Douban api:
The meaning of the status code indicates that the 200ok request succeeded 201created created successfully 202accepted updated successfully 400bad request request does not exist 401unauthorized unauthorized 403forbidden forbids access to 404not found resources does not exist 500internal server error internal error 5. Use a common error code
General error code, specific products are given by the specific product api. For example, Douban api:
Error code error message meaning 999unknow_v2_error unknown error 1000need_permission requires permission 1001uri_not_found resource does not exist. ..
There are too many, only a few are listed, see Douban api for details.
6. Safety
This part does not belong to this article, but let's explain a little bit:
Use https
Validate using jwt
Use parameter signatures to prevent parameters from being tampered with.
Use permission authentication, shiro, or build your own database (users, roles, permissions)
7.api document
The writing of interface documentation is very important, and it is best to write an online interface document. Interface documents can be easily consulted by the team and reduce unnecessary communication. If the quality of public api,api documents directly reflects a company's technical level, or even a company's cultural temperament.
After reading the above, do you have any further understanding of how to understand the Restful API design according to Douban api? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.