The RESTful API uses a detailed

Source: Internet
Author: User
Tags error handling

RESTful API is currently a relatively mature set of Internet application API design concepts,rest is a set of architectural constraints and principles, how to rest constraints and principles of the architecture, We call it a restful architecture , with a well-structured, standards-compliant, easy-to-understand, and easy-to-use architecture that is being used by more and more websites!

The Restful API Interface specification includes the following sections:

First, the agreement

The API communicates with the user protocol, always using the HTTPs protocol.

Second, the domain name

You should try to deploy the API under a dedicated domain name, such as https://api.oldboyedu.com; If you determine the API is simple, there will be no further expansion, you can consider placing it under the primary domain name, such as https://oldboyedu.com/api/.

Third, version

You can put the version number in the HTTP header information, or you can put it in a URL , such as https://api.oldboyedu.com/v1/

Iv. Path

A path is an address that behaves as a Web site on the Internet, in a RESTful architecture where each URL represents a resource (resource), so there can be no verbs in the URL, only nouns, and nouns often correspond to the table names in the database. In general, the tables in the database are " collections "(collection) of the same record, so nouns in the API should also use complex numbers, such as https:// Api.oldboyedu.com/v1/students.

Five,HTTP verb

For the specific operation type of the resource, represented by the HTTP verb, theHTTP verb mainly has the following, the corresponding SQL command in parentheses.

1. GET(SELECT): Remove resources from the server (one or more items);

2. POST(create): Create a new resource on the server;

3. PUT(update): Update the resources on the server (the client provides the changed full resources);

4. PATCH(Update): Updates the resource on the server (the client provides the changed properties);

5. Delete:Delete the resource from the server;

6. HEAD: Get the metadata for the resource;

7. OPTIONS: Get information about which properties of a resource are available to the client to change.

VI. Filtering Information

If the number of records is large, the server will not be able to return them to the user, theAPI will provide parameters to filter the return results, common parameters are:

1. Limit=20: Specifies the number of returned records;

2. Offset=8: Specifies that the starting position of the returned record is 8;

3. Page=1&per_page=50: Specify page 1 and the number of records per page;

4. SORTBY=NAME&ORDER=ASC: Specifies that the returned results are sorted in ascending order by the name attribute;

5. animal_type_id=2: Specify the filter criteria.

Seven, status code

The server returns status codes and prompts to the user, and the following are some common status codes:

1 OK-[GET]: The server successfully returns the data requested by the user;

2.201 CREATED-[Post/put/patch]: User new or modified data success;

3.202 Accepted-[*]: Indicates that a request has entered the background queue (asynchronous task);

4.204 NO CONTENT-[delete]: User deleted data successfully;

5. INVALID request-[Post/put/patch]: The user has made an error, the server does not make new or modified data operation;

6.401 Unauthorized-[*]: Indicates that the user does not have permission (token, user name, password error);

7.403 Forbidden-[*] indicates that the user is authorized (as opposed to the 401 error), but access is prohibited;

8.404 Not FOUND-[*]: User requests for non-existent records, the server does not operate;

9.406 Not acceptable-[GET]: User requested format is not available;

410 Gone-[get]: The resource requested by the user is permanently deleted and will no longer be available;

422 unprocesable Entity-[post/put/patch] when an object is created, a validation error occurs;

A . INTERNAL Server Error-[*]: The server has errors, the user will not be able to determine whether the request was made successfully.

Eight, error handling

If the status code is 4xx, an error message is returned to the user, in general, the error is used as the key name and the error message as the key value in the returned information.

Ix. Return of results

For different operations, the results returned by the server to the user should conform to the following specifications:

1. Get/collection: Returns a list of resource objects (arrays);

2. Get/collection/resource: Returns a single Resource object;

3. Post/collection: Returns the newly generated resource object;

4. Put/collection/resource: Returns the complete resource object;

5. Patch/collection/resource: Returns the complete resource object;

6. Delete/collection/resource: Returns an empty document.

Ten,Hypermediaapi

The RESTful API is best done by Hypermedia, which provides links to the returned results, connecting to other API methods, so that users do not look up documents and know what to do next.

The above are the ten standards that should be followed by RESTful API design, besides, the RESTful API should also note that authentication should use the OAuth 2.0 framework, the data format returned by the server, should use JSON as much as possible, and avoid using XML.

The RESTful API uses a detailed

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.