Restful API design specifications and actual combat

Source: Internet
Author: User

The concept of Restful API here does not bother, Bo friends online search ha definition articles a lot, straight into the point:

First, throw a question:
Judging by the ID of the user, the name is the product of Call of Duty (COD14) exists (say I still like to play like the World War II mission call this amount, off-topic ...)? If this problem occurs in an MVC project, I think we will generally design this:

Api/products/isexist/{userid}/{productname}

I think you should find some problems, this writing is completely MVC, but it does not apply to WebAPI, there are three main questions:
The Route definition is confusing and completely violates some of the design principles of the REST API URI. The Action is not named properly. The bool return value is not appropriate. For the above three questions, we will discuss the following separately.

1. Uri design first of all, we know that in the REST API, the URI represents a resource, it is designed to meet two basic requirements, the first word rather than the verb, the second to clearly express the meaning of resources,

In other words, from a URI, you can be very straightforward to know what resources are being accessed, and we'll look at the URI we designed:

Api/products/isexist/{userid}/{productname}

This is God horse thing ah??? This design completely violates the URI principle, first of all, let's comb it out, what is the resource we want to request? Yes, it is a product (products), but this product is a certain user,

So users and products have a subordinate relationship, access to the product first to access the user, this is reflected in the URI, and secondly, we are to obtain products? Or do you judge whether the product exists? This concept is different,

The unique identity of the product is the same as the user ID, in the general design of the URI, if you want to access a resource under a unique identity (such as product with ID 1), this is the design:

Api/products/{id}

The HttpGet method (API/PRODUCTS/1) is used in the HttpClient request, so we can get an product with an ID of 1, but now the scenario is that the acquisition is not a unique identity, but rather through the product name, do we design:

Api/products/{productname}

I look at it, it seems that there is no problem with the design, but always feel something wrong, such as if you add a product size, it should be changed to this: Api/products/{productname}/{productsize}, this design is completely inappropriate, said,

The URI represents a resource, and the only way to get a resource through a URI is through a resource's unique identity, except that the fetch can be considered a query for the resource, so for our scenario, the URI should be designed like this (correct):

Format standard: api/users/{userid}/products: Example  : API/users/1/products?productname= Call of Duty COD14

The above URI is clear: The query ID is a product named COD14 under 1 users.

Restful API design specifications and actual combat

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.