What is the restful API?

Source: Internet
Author: User
Tags representational state transfer
What is the restful API.

To figure out what the restful API is, first figure out what rest is. REST-Representational State Transfer, the literal translation of the English language is "the Expression layer status transfer". If you look at this concept, it is estimated that few people can understand what it means. Well, let me explain in one sentence. What is a Restful:url locating resource, describing the operation with an HTTP verb (get,post,put,delete).


Resource: Resources, that is, data.
Representational: Some form of expression, such as the use of json,xml,jpeg;
State Transfer: status change. Implemented through an HTTP verb.


So the RESTful API is the rest-style API. So what scenario is using the RESTful API? In today's Internet applications, the front-end display media is very rich. There are mobile phones, tablets and PCs and other display media. Then these front-end received user requests unified by a background to process and return to the different front-end is certainly the most scientific and the most economical way, the RESTful API is a set of protocols to standardize the various forms of front-end and the same background interaction.

The RESTful API is invoked by the backend, which is the server that provides the front-end. The front-end invoke API initiates an HTTP request to the background, and the background response request feeds the processing results to the front end. That is to say restful is a typical HTTP based protocol. So what are the design principles and specifications for the RESTful API?


1, resources. The first is to figure out the concept of resources. A resource is an entity on a network, a piece of text, a picture, or a song. A resource always responds to its content by means of a carrier. Text can be txt, or HTML or XML, images can be in JPG or PNG format, and JSON is now the most common form of resource expression.


2, unified interface. RESTful-style data meta-manipulation crud (create,read,update,delete) corresponds to the HTTP method: Get is used to obtain resources, post is used to create new resources (also can be used to update resources), put is used to update resources, delete is used to delete resources , thus unifying the interface for data manipulation.


3,uri. You can point to a resource with a URI (Uniform Resource Locator), that is, each URI corresponds to a specific resource. To get this resource to access its URI, the URI becomes the address or identifier of each resource. In general, each resource has at least one URI corresponding to it, and the most typical URI is the URL.


4, no State. The so-called stateless means that all resources can be URI-positioned, and that this location is independent of other resources and will not change as a result of other resources. The difference between stateful and stateless, for example, the step to query employee pay is the first step: Log in to the system. Step Two: Enter the page to inquire salary. Step three: Search for the employee. Fourth step: Click the name to view the salary. Such an operation process is stateful, every step of the query pay depends on the previous step, as long as the predecessor operation is unsuccessful, subsequent operations can not be performed. If you enter a URL to get a specified employee's salary, this situation is stateless because the salary is not dependent on other resources or state, and in this case, the employee's salary is a resource, and the corresponding URL can be used to get the resource through the Getting method in HTTP, This is the typical restful style.


Said so much, exactly what restful look like.

Get:http://www.xxx.com/source/id gets a certain type of resource for the specified ID. For example, get:http://www.xxx.com/friends/123 represents getting a buddy list of members with ID 123. If you don't add an ID, you get a buddy list of all the members.

Post:http://www.xxx.com/friends/123 represents a new friend for a member with a specified ID of 123. Other operations like this do not give examples.


The

    restful API also has some other specifications. 1: The API version number should be placed in the URL. Get:http://www.xxx.com/v1/friend/123. or place the version number in the HTTP header information. I personally feel that the version number depends on the habits of the development team and the needs of the business, not mandatory. 2:url can only have nouns but not verbs, and the expression of operations is get,post,put,deletel using the verb of HTTP. The URL identifies only the address of the resource, and since it is a resource it is a noun. 3: If the number of records is large, the server cannot all return them to the user. The API should provide parameters that filter the return result.? limit=10: Specify the number of records returned,? PAGE=2&PER_PAGE=100: Specifies the first few pages, and the number of records per page.

Related Article

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.