What exactly is REST, RESTful?

Author: Tan Ultra
link: https: //www.zhihu.com/question/28557115/answer/48094438
Source: know almost
copyrighted by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

I think the problem is very good: REST - REpresentational State Transfer direct translation: presentation layer state transfer. This Chinese literal translation often appears in many blogs. Nima, who understands "state transfer of the presentation layer"? Is this human? I myself have been confused for a long time, and after searching a lot of information, it took almost a year to get a fairly clear understanding. Share as follows:

The teacher's one sentence summary is very incisive:
URL locates resources and uses HTTP verbs (GET, POST, DELETE, DETC) to describe operations.

--- Concise version ---

0. REST is not the word "rest", but a few word abbreviations. But even if those few words are spoken, I can’t understand what I’m talking about-_-!! (not to belittle people, it’s difficult for me to understand myself);
1. REST describes the interaction between client and server in the network Form; REST itself is not practical, what is practical is how to design a RESTful API (REST-style network interface);
2. In the RESTful API provided by Server, only nouns are used to specify resources in the URL, and verbs are not used in principle. "Resources" are the core of the REST architecture or the entire network processing. For example:
api.qc.com/v1/newsfeed: Get someone's freshness;
api.qc.com/v1/friends: Get a list of someone's friends;
api.qc.com/v1/profile: Get someone's detailed information; 3. Use the verbs in the HTTP protocol to add, modify, and delete resources. That is, the state of resources is reversed through HTTP verbs:
GET is used to obtain resources,
POST is used to create new resources (and can also be used to update resources),
PUT is used to update resources, and
DELETE is used to delete resources. For example:
DELETE http://api.qc.com/v1/friends: delete someone's friend (specify friend id in http parameter)
POST http://api.qc.com/v1/friends: Add friends
UPDATE api.qc.com/v1/profile : update personal information

Prohibited to use: GET api.qc.com/v1/deleteFri 图例:
<img src="https://pic1.zhimg.com/7405939b62a73f28846533de08db3a80_b.jpg" data-rawwidth="1328" data-rawheight="702" class="origin_image zh-lightbox-thumb" width="1328" data-original="https://pic1.zhimg.com/7405939b62a73f28846533de08db3a80_r.jpg">

4. A representation of a certain resource is transferred between Server and Client, such as using JSON, XML to transfer text, or using JPG, WebP to transfer pictures, etc. Of course, you can also compress the data during HTTP transmission (on-wire data compression).
5. Use HTTP Status Code to transmit the server status information. For example, the most commonly used 200 means success, 500 means internal server error, etc.

That's the main message. Finally, we need to liberate our minds. Instead of using the typical PHP or JSP architecture, the Web side will be replaced with front-end rendering and incidental business logic (such as some examples of AngularJS or BackBone). The Web and Server only use the above-defined API to transfer data and change data status. The format is generally JSON. The same applies to iOS and Android. It can be seen that Web, iOS, Android and third-party developers have become equal roles to jointly consume the services provided by Server through a set of APIs.


--- Detailed version---

Let me start with the name
REST - REpresentational State Transfer.
First of all, the reason for the obscurity is that the previous subject has been removed, and the full name is Resource Representational State Transfer: in layman's terms, it means that resources are transferred in a certain form in the network. Break it down:
Resource: Resources, that is, data (the core of the network was mentioned earlier). Such as newsfeed, friends, etc.;
Representational: a certain form of representation, such as JSON, XML, JPEG, etc.;
State Transfer: state change. Through HTTP verbs.
The source of REST
Roy Fielding's graduation thesis. This buddy participated in the design of the HTTP protocol, and is also the co-founder of the Apache Web Server project (unfortunately, it is now the world of nginx). PhD graduate school is UC Irvine, Irvine is in California, with abundant sunshine and beautiful beaches, it is a famous rich area. The headquarters of Oculus VR is located here (virtual reality glasses, acquired by FB, CTO is John Carmack, author of Quake and Doom).
Everyone knows that the papers are obscure. When I was studying at CMU, many courses would arrange two paper reviews a week. In retrospect, every time I write a Paper review is my most painful time. The doctoral thesis of REST is undoubtedly more.
Paper address: Architectural Styles and the Design of Network-based Software Architectures
REST章节: Fielding Dissertation: CHAPTER 5: Representational State Transfer (REST)
I read the REST chapter for the first time, but I did not finish reading the whole paper=_=
<img src="https://pic3.zhimg.com/11cdfc60bde58e8545bafe42f0af79ca_b.jpg" data-rawwidth="500" data -rawheight="375" class="origin_image zh-lightbox-thumb" width="500" data-original="https://pic3.zhimg.com/11cdfc60bde58e8545bafe42f0af79ca_r.jpg">

What RESTful API is
practical is how to correctly understand RESTful architecture and design RESTful API.

Why use RESTful structure in the first place?
Everyone knows that "ancient" web pages are a combination of front and back ends, such as PHP and JSP before. In the previous desktop era, the problem was not big, but in recent years, with the development of the mobile Internet, various types of clients have emerged one after another. RESTful can provide services for the Web, iOS and Android through a unified interface. In addition, for the majority of platforms, such as Facebook platform, Weibo open platform, WeChat public platform, etc., they do not need to have an explicit front-end, but only need a set of service interfaces, so RESTful is their best choice. Under RESTful architecture:
<img src="https://pic2.zhimg.com/06ee404783540f0af299042057738a99_b.jpg" data-rawwidth="550" data-rawheight="250" class=" origin_image zh-lightbox-thumb" width="550" data-original="https://pic2.zhimg.com/06ee404783540f0af299042057738a99_r.jpg">
How is the Server's API designed to meet RESTful requirements?
First of all These are the points in the concise version. Plus some accompanying best practices:
1. URL root:
example.org/api/v1/*
api.example.com/v1/ *2. API versioning: It
can be placed in the URL or HTTP header:
/api/v1/
3. Use nouns instead of verbs in URIs, and plurals are recommended.
BAD
  • /getProducts
  • / listOrders
  • /retrieveClientByOrder?orderId=1
GOOD
  • GET /products : will return the list of all products
  • POST /products : will add a product to the collection
  • GET /products/4 : will retrieve product #4
  • PATCH/PUT /products/4 : will update product #4

4. Ensure that the HEAD and GET methods are safe and will not change the resource state (pollution). For example, strictly prohibit the following situations:
GET /deleteProduct?id=1
5. The address of the resource is recommended to use a nested structure. For example:
GET /friends/10375923/profile
UPDATE /profile/primaryAddress/city 6. Beware of the size of the returned result. If it is too large, pagination or limit should be added in time. HTTP protocol supports Pagination operation, just use Link in Header.
7. Use the correct HTTP Status Code to indicate the access status: HTTP/1.1: Status Code Definitions
8. Use clear and easy-to-understand text (String. Note that the returned error is for people to see, avoid using 1001 error messages), and add comments appropriately.
9. About security: Use https for your own interface, add a key to make a hash and put it at the end. Taking into account the national conditions, HTTPS is unstable in wireless networks, and the entire HTTP payload can be encrypted using Application Level encryption methods. Friends who are interested can use their mobile phones to connect to the computer’s shared Wi-Fi, and then use Charles to listen to WeChat network requests (post photos or scan Moments).
If it is a platform API, you can use mature but complicated OAuth2, Sina Weibo article: Authorization mechanism description

The specific implementation of each end
As shown in the figure above, Server provides a set of RESTful API uniformly, web+ios+android calls the API as equivalent citizens. Up to now, each end has developed a relatively mature framework to help developers get twice the result with half the effort.

- Server
--Recommended: Spring MVC or Jersey or Play Framework
Tutorial:
Getting Started · Building a RESTful Web Service

- Android -
Recommended: RetroFit ( Retrofit) Or Volley ( mcxiaoke/android-volley · GitHubGoogle’s official block is not posted)
Tutorial:
Retrofit โ€” Getting Started and Create an Android Client
Retrofit of Rapid Android Development Series Network

- iOS -
Recommended: RestKit ( RestKit/RestKit · GitHub )
教程:
Developing RESTful iOS Apps with RestKit

- Web - It is
recommended to do it casually! You can use the heavyweight AngularJS, or you can use the lightweight Backbone + jQuery.
Tutorial: blog.javachen.com/2015/

参考:
[1]: Some REST best practices
[2]: GitHub API v3
[3]: tlhunter/consumer-centric-api-design · GitHub

Finally, there is an egg:
Facebook Billiards Performance: Billiards 1—Play online

Guess you like

Origin blog.csdn.net/Qianliwind/article/details/75042251