How to design a programmer's own UI-restful interface design.

Source: Internet
Author: User
Tags representational state transfer

Recently I am working on a client program architecture design. I read the server's first interface document and found that the interface design is not very good. There are no specifications or rules for it. I made some modifications and gave some suggestions.

Now I want to share my experiences with you. I hope that you can follow these specifications when designing APIs in the future, so that our programmers can be more comfortable with coding.

 

The so-called restful architecture is the representational state transfer. But in fact, this phrase lacks a subject, that is, resources. Resources. In combination, the status of the resource presentation layer is converted.

It is hard to understand. Let me explain it.

Resources are understandable. For example, text, images, and videos on the network are all resources.

The representational presentation layer represents these resources. For example, text can be expressed in TXT in HTML and JSON in XML. Jpg png can be used for images.

 

At this point, pay attention to our Uri. It can only represent the resource entity, but not the resource representation. For example, many of our web pages will be followed by a. html address. In fact, this is very bad.

Style .. HTML is a representation of resources. This is a format. However, the URI can only represent the resource location. Therefore, the form. html should be placed in the accept and Content-Type of the HTTP request header.

Here. These two fields are the expressions of representational.

 

Finally, the state transfer can only perform put Delete get and post operations on resource URIs in the HTTP world. This represents the conversion of resources.

 

The following example shows how to design a restful API or Uri.

 

1. No verb exists in Uri.

The verb should be placed in the HTTP header. For example,/news/show/1

This URI is poorly designed. Because show is a verb. Therefore, this URI should be designed as/news/1. Uri can only represent resources.

Then this show is placed in the HTTP header, that is, the get method.

 

2. What if the four HTTP words put Delete get post in some operations do not work.

For example, you need to design an operation for a player to remit 200 yuan to B.

Some people design

Post/accounts/A/transfer/200/to/B

But the restful style does not allow us to place verbs in the URI, so we should

Post/transaction HTTP/1.1

HOST: 127.0.0.1

From = A & to = B & amount = 200

In this way, the style is perfect.

 

3. How to express the version number.

Server interfaces are often upgraded, so each interface has a version number. Sometimes, when accessing the client, the client sends the server interface version number used by the client to the server to determine how to respond.

Many people will design it like this.

Http://www.burning.com/app/1.0/test

But the style is actually poor. We should put the version number in. The accept field in the HTTP header.

 

4. How to request multiple resources at a time. How to design such a URI.

For example, we can

REQUEST:GET /hotel/656bcee2-28d2-404b-891b/classification,name HTTP/1.1Host: localhostUser-Agent: xLightweb/2.6Accept: application/x-www-form-urlencodedRESPONSE:HTTP/1.1 200 OKServer: xLightweb/2.6Content-Length: 43Content-Type: application/x-www-form-urlencoded; charset=utf-8classification=Comfort&name=Central

 

Note that

Restful HTTP server programs must return status codes according to HTTP specifications. The first digit of the status code indicates the return type. 1xx indicates a temporary response, 2XX indicates a successful response, 3xx indicates forwarding, 4xx indicates a client error, and 5xx indicates a server error. It is not a good practice to use an error response code or always return a 200 response and include the response of a specific application in the message body.

 

How to design a programmer's own UI-restful interface design.

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.