One article takes you understand what is RESTful API

First, what is API

To know what is RESTful API, we must first know what is API.

API (Application Programming Interface, application program interfaces) are some pre-defined function , or to different components of the software system convergence conventions. [1] The purpose is to provide the application developer based on a software or hardware to access a set of routines that capacity, but without accessing the original code, or understand the inner workings of the mechanism details. (Quoted from Baidu Encyclopedia)

For example: For example, we go to KFC to buy a hamburger, hamburger does not need to know this is how to do it, then you will be able to pay any money to get a burger.

KFC is a server, you are a client, the money that you request to obtain the parameters KFC Hamburg.

For example, if you want to water, you do not have to produce water, you only need to call the Waterworks good water on the line, put the water to the water pipes inside your home, you just turn on the tap on the line, the water will come out . You open the hose is in the process of calling Waterworks API.

API calls that have paid their dues in one hand and delivery process, making Hamburg is working behind the API.

ps: the above is to say that people do not understand computers explain what API Yes.

With computer terms, that is, programs use the access code to write code written by others is a channel API.

For example, you want to program Alipay payment functions, if you want to implement your own payment function, you have to go to the bank docking ah, you have to pay a license application, and then write a program, a series of down, you can set up a finance company , but now you do not want to set up a finance company that you just want to use Alipay payment functions, how to do it, you can use the interface Alipay, call time payment functions in your program which, in fact, requested Alipay payment functions, you do not have to consider the payment function Alipay is how to do, what language to achieve, you just follow the specification request Alipay payment request on the line. This is the API call Alipay.

Second, what is REST

That REST Representational state transfer (English: Representational State Transfer, REST for short) is proposed by Dr. Roy Fielding in his doctoral thesis in 2000 of one software architecture style. For it is a web application design and development method, development can reduce the complexity, improve system scalability. (Quoted from Baidu Encyclopedia)

Representational state transfer, What is that, who can understand ah, this literal translation in English and I vomited.

  • Is in the form of an interactive network of client and server, REST is not a protocol does not have much effect, is how practical design RESTful API (REST-style interfaces) REST described

Why use it RESTful architecture

Web pages are previously melt together the front and rear ends, such as JSP and the like before. In the PC era is not the issue before the Sha problem, but in recent years the rapid development of mobile Internet, particularly various front-end framework, if we do not separate the front and rear end, then, is learning a particularly high cost, a repetition rate is too high lead to code we do a lot of repetitive stuff, code reuse rate is not high, by way of an interface that allows code reuse rate becomes higher, as shown below

Interface invocation
Why not use common interface mode, for example, you want to get your website on top of the weather of a city, you have to be so before it is possible to design

https://xxx/getWeather?city=深圳

So you have to pass two parameters to the background, so it looks very bloated.

RESTful interface design with the way you may be so designed

GET https://xxx/weathers/深圳

So only need to pass a parameter to the background on it, so it looks very simple, and our use of the URI is a noun, not a verb. Jump to realize resource via HTTP verbs. Said the following specific implementation.

These are the reasons why we want to use RESTful API structure.

Third, how to design the system architecture of a RESTful API yet.

  1. URI is used inside a noun and not a verb, it is recommended to use the plural, to achieve a jump of resources via HTTP verbs.

    • Incorrect

      /getOrders

      listCitys

      /getWeathers?city=深圳

    • correct

      GET /orders/1 : Returns the order number for the order of 1

      POST /orders : Add an order

      Delete /orders/1 : Delete an order number for the order of 1

      PUT /orders/1 : Update order number for the order of 1

  2. Assurance methods which only one thing will not change the state of the resource. For example follows is not allowed

GET /updateOrder?id=1

  1. Using the correct HTTP Status Code indicates the status of the request to return. such as

    {"code":"200"}

The above is an example of how to design a simple RESTful API structure of the system.

Tips:

Taiwan before and after the data transfer can be used json, xml can transmit, I still tend to transfer more convenient json

Temperature such as requesting a city, with the result returned is xml

<city>   
    <name>深圳</name>
    <temperature>26</temperature>
     <code>200</code>
</city>

Return is such a json

{
    "city":
    {
        "name":"深圳",
        "temperature":"26",
        "code":200
    }
}

If you think okay to write it, I beg you point a praise it.
I'm looking for the exchange of words, I welcome the attention of the public number: Thousand Jue, background message to me.

Published 11 original articles · won praise 75 · views 6949

Guess you like

Origin blog.csdn.net/zjwl199802/article/details/103595652