Reprint: How to explain rest in plain language and restful?

Source: Internet
Author: User
Tags representational state transfer

Shup
Links: https://www.zhihu.com/question/28557115/answer/48094438
Source: Know
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.

The URL locates the resource and describes the operation with an HTTP verb (get,post,delete,detc).

---simple version---

0. Rest is not the word "rest", but a few acronyms. But even if those words come out, it doesn't make sense to say what-_-!! (not to belittle people, but to understand difficulties myself);
1. Rest describes a form of interaction between the client and server in the network; Rest itself is impractical, and practical is how to design restful APIs (restful network interfaces);
2. In the RESTful API provided by the server, only nouns are used in URLs to specify resources, and verbs are not used in principle. A "resource" is the core of the rest architecture, or the entire network processing. Like what:
http://Api.qc.com/v1/newsfeed to get someone's freshness;
http://Api.qc.com/v1/friends to get a list of someone's friends;
http://Api.qc.com/v1/profile to get someone's details; 3. The use of verbs in the HTTP protocol to achieve the addition of resources, modification, deletion and other operations. The use of HTTP verbs to achieve the state of the resource reversal:
Get is used to get resources,
POST is used to create a new resource (which can also be used to update resources)
PUT is used to update resources,
Delete is used to remove resources. Like what:
Delete Http://api.qc.com/v1/friends: Remove someone's friend (Specify a friend ID in HTTP parameter)
POST http://api.qc.com/v1/friends: Add Friends
Update/ http:api.qc.com/v1/profileUpdates profile

Prohibit use: GEThttp/api.qc.com/v1/deletefriend Legend:

4. A representation of a resource is passed between server and client, such as transferring text with Json,xml, or transferring pictures with JPG,WEBP. Of course, you can also compress the data when the HTTP is transmitted (On-wire compression).
5. Use the HTTP status code to pass the server's status information. For example, the most commonly used 200 indicates success, 500 indicates a server internal error, and so on.

The main message is just this point. Finally, to liberate the mind, the Web side is no longer using the typical PHP or JSP architecture, but instead to the front-section rendering and the accompanying simple business logic (such as ANGULARJS or backbone some examples). The Web side and server use only the APIs defined above to pass data and change the state of the data. The format is generally JSON. iOS and Android are the same. Thus, web,ios,android and third-party developers become equal roles through a set of APIs to collectively consume server-provided services.


---detailed version--- say the rest name firstREST--Representational State Transfer
First of all, the reason is obscure because the subject is removed, the full name is Resource representational State Transfer: In layman's terms: the resources in the network in some form of representation of the status transfer. Break apart:
Resource: The resource, the data (the core of the network mentioned above). such as newsfeed,friends and so on;
Representational: Some form of expression, such as with Json,xml,jpeg;
State Transfer: status change. Implemented via HTTP verbs.
the source of restRoy Fielding's graduation thesis. This buddy is involved in the design of the HTTP protocol and is also the co-founder of the Apache WEB Server project (which is now the Nginx world). PhD's Graduate School is UC Irvine,irvine in California, with plenty of sunshine and beautiful beaches, and is famous for the wealthy district. The headquarters of Oculus VR is located in this (virtual reality glasses, acquired by FB, CTO for Quake and Doom's author John Carmack).
Controversial is known to all, the paper is obscure and difficult to understand. When studying at CMU, many of the courses were scheduled to be paper review two times a week. Now in retrospect every time I write paper review is the most painful time. Rest of this doctoral dissertation is no doubt more.
Paper Address: Architectural Styles and the Design of network-based software architectures
Rest Chapter: Fielding dissertation:chapter 5:representational State Transfer (rest)
I read the rest chapter, and I didn't finish the whole paper =_=

RESTful API
What's practical is how to properly understand the restful architecture and design restful APIs.

Why first use a restful structure? We all know that the "ancient" Web page is the front end of the merged together, such as the previous php,jsp and so on. The previous desktop era is not a big problem, but in recent years the development of mobile Internet, various types of client endless, restful can be a unified interface for Web,ios and Android services. In addition to the vast number of platforms, such as Facebook platform, Weibo open platform, public platform, they do not need to have an explicit front-end, only a set of services to provide the interface, so restful is their best choice. Under the RESTful architecture:

How does the server's API design meet restful requirements?
The first is the concise version of the inside of those points. Plus some of the accompanying best practices:
1. URL Root:
https://example.org/api/v1/ *
https://api.example.com/v1/ * *. API Versioning:
Can be placed in the URL, you can also use the HTTP header:
/api/v1/
3. URIs use nouns instead of verbs and are recommended in plural.
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: would retrieve product #4
    • PATCH/PUT/PRODUCTS/4: would update product #4

4. Ensure that the HEAD and GET methods are secure and do not change the state of the resources (pollution). For example, the following conditions are strictly eliminated:
Get/deleteproduct?id=1
5. The address of the resource is recommended with nested structures. Like what:
Get/friends/10375923/profile
Update/profile/primaryaddress/city6. Be wary of the size of the returned results. If it is too large, make a timely paging (pagination) or join limit. The HTTP protocol supports paging (pagination) operations, using Link in the header.
7. Use the correct HTTP status code to indicate access status: Http/1.1:status code definitions
8. In the returned results with clear text (String. Note that the error returned is to be seen, avoid using 1001 of this error message, and add comments as appropriate.
9. About security: Your own interface with HTTPS, plus a key to do a hash at the end can be. Considering the national conditions, HTTPS is unstable in the wireless network, you can use the application level encryption method to encrypt the entire HTTP payload. Interested friends can use the phone to connect to the computer's shared Wi-Fi, and then use Charles to listen to the network requests (send photos or brush friends circle).
If it is the platform API, can use mature but complex OAuth2, Sina Weibo this article: authorization mechanism explanation

Specific implementations of each end
As shown in the figure above, server unified provides a set of restful api,web+ios+android called APIs as equal citizens. At each end of the development, there is a more mature framework to help developers do more.

--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 GitHub Google's official block, will not be posted)
Tutorial:
Retrofit??? Getting Started and Create an Android Client
Retrofit of the Fast Android Development Series Network article

--IOS--
Recommendation: RestKit (Restkit/restkit GITHUB)
Tutorial:
Developing RESTful IOS Apps with RestKit

--Web--
Recommended Casual! You can use heavyweight angularjs, or you can use lightweight Backbone + jQuery.
Tutorial:/http/blog.javachen.com/2015/01/06/build-app-with-spring-boot-and-gradle/

Reference:
[1]: Some REST Best Practices
[2]: GitHub API v3
[3]: Tlhunter/consumer-centric-api-design GitHub

Reprint: How to explain rest in plain language and restful?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.