A rest-style discussion: people know why they are right only after suffering!

Source: Internet
Author: User
My colleagues recently complained that I have provided a lot of APIs and it is hard to remember. Because my URL is like this: in the traditional URL mode, for example, if I want to participate in an activity, the URL may be POST: myapp.com. cn1api12 then, if you want to obtain a list of people participating in the Plan, the URL can be... my colleagues recently complained that I have provided a lot of APIs and it is hard to remember.
Because my URL is like this:
In the traditional URL mode, for example, if I want to participate in an activity, the URL may be
POST:myapp.com.cn/1/api/12
Then, if you want to obtain a list of participants, the URL may be:
POST:myapp.com.cn/1/api/13
This method is obviously inappropriate. This is a business addition, deletion, modification, and query. You should use a URL.
I encountered this problem when writing APIs. There will be a lot of business codes for adding, deleting, modifying, and querying.
In fact, you should use the GET method to obtain the list, use POST to participate in an activity, and use DELETE to exit an activity.
This is the rest style.

It's only after these experiences that we know why people are right !!!!! The knowledge in books has to be put into practice. Only by taking a detour can we know what is right and what is wrong. It's like someone asked me before. Whether data verification is written on the model layer or the controller layer. I told him that you should give it a try, and you will naturally know that it is better to write it on that layer.
But now I think writing on the model layer is not the best. It is good to directly split a data verification layer. Why? Because I have practiced

Reply content:

My colleagues recently complained that I have provided a lot of APIs and it is hard to remember.
Because my URL is like this:
In the traditional URL mode, for example, if I want to participate in an activity, the URL may be
POST:myapp.com.cn/1/api/12
Then, if you want to obtain a list of participants, the URL may be:
POST:myapp.com.cn/1/api/13
This method is obviously inappropriate. This is a business addition, deletion, modification, and query. You should use a URL.
I encountered this problem when writing APIs. There will be a lot of business codes for adding, deleting, modifying, and querying.
In fact, you should use the GET method to obtain the list, use POST to participate in an activity, and use DELETE to exit an activity.
This is the rest style.

It's only after these experiences that we know why people are right !!!!! The knowledge in books has to be put into practice. Only by taking a detour can we know what is right and what is wrong. It's like someone asked me before. Whether data verification is written on the model layer or the controller layer. I told him that you should give it a try, and you will naturally know that it is better to write it on that layer.
But now I think writing on the model layer is not the best. It is good to directly split a data verification layer. Why? Because I have practiced

The relationship between table operations and REST is not correctly understood. The following is a question I have answered before.

Address: http://www.zhihu.com/question/21662167/answer/18918959

The landlord should have a basic understanding of REST, so I will not repeat the basic concepts. I just want to explain what the landlord is confused about.

Resources are not directly mapped to underlying storage objects or program models.
It doesn't mean that you have a User table and a Role table, you must design the corresponding resources.
In fact, the relationship between RESTful resources and underlying storage services is similar to the relationship between tables and views in relational databases. A view is a set of relationships formed by combining multiple tables according to the actual Query Needs.
Whether your storage service is a relational database, NoSQL database, or even text file, it is the same for the client that accesses resources.

Therefore, to create a user and set its role, you can directly use POST/user.

// Create a new user with the foo and bar roles
POST/user
{Name: (string), passwd: (string), roles: ['foo', 'bar']}
// It is best if the response header can contain the following two
// Respond in the 201 status and use Location to notify the new resource url
HTTP/1.1 201 Created
Location:/user/1

// Modify the role of a user to foobar.
PUT/user/1
{Roles: ['foobar']}

// Modify the user's password
PUT/user/1
{Passwd: (string )}

For resources with a small granularity such as/UserRoleRelation, I suggest you do not first. The granularity of resources should be rough and fine, based on the subsequent business evolution and actual needs, we need to consider whether to abstract more fine-grained resources. If we get too detailed at the beginning, any operation will be broken down into multiple network I/O operations, and the system complexity is relatively high.

Map a specific database table to a resource, and then map the CRUD action to GET/POST/PUT/DELETE, Which is silly and insecure. These items are originally used for business services, because of business requirements, the results are abstracted but not designed around the business.

The correct idea should be: what databases and program models should be forgotten, and what resources (URLs) should be designed based on the service from the HTTP perspective ), what parameters are accepted and responded to in GET/POST? After finalizing these parameters, we will consider how to cooperate with the database and program Model.

I recommend that you learn about the API design of Github and Dropbox. I personally think it is better than Twitter.

Well, if you don't understand it, shouldn't you first look at how others are doing it, and see if there are any mature solutions. This is twitter's api structure.

Https://dev.twitter.com/rest/public

Standard Guide for building APIs using JSON translated a few days ago: Chinese Version

Thank @ bornkiller for assisting in translation.

I don't know which book does your original understanding of REST come from, but read more books and read a good book at the same time.

The author of the REST architecture wrote an article in, accusing people of lack of understanding about REST. REST (Representational State Transfer/declarative State Transfer) is not only the GET ing between GET/POST/DELETE/PUT and Create Read Update Delete operations in the DataTable.

The reason why people think of RESTful As the CRUD of a table may be due to the impact of Rails on REST support. According to Roy Fielding, RESTful must be Hypertext-driven (using hypermedia as the engine of application status), but according to the definition of his old man, the REST APIs advertised in 90% cannot say they are RESTful. Therefore, Richard grades the REST model, and most RESTful APIs focus on the level-1 maturity, the REST design described in the Fielding paper belongs to the three levels of the Web-aware and hierarchical model.

Therefore, I would like to remind you that you are RESTful instead of a domain/Res/ID. Now that you want to play REST, you have to read at least one of the original author's first-draft papers. Even if you have read only a few insights, You won't design the first API in your description.

The top landlord, practice really knows, just rush at this, don't like it.

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.