What is an API

To know what a RESTful API is, we need to know what an API is.

Application Programming Interfaces (apis) are predefined functions or conventions that refer to the Interface between different components of a software system. [1] The goal is to provide applications and developers with the ability to access a set of routines based on a piece of software or hardware without having to access the source code or understand the details of the inner workings. (Quoted from Baidu Baike)

For example, if we go to KFC and buy a hamburger, we don’t need to know how it is made, you pay for it and you get a hamburger.

KFC is a server, you are a client, money is your request to KFC to obtain the parameters of the burger.

For example, if you want to use water, you don’t have to produce your own water, you just need to call the water produced by the water factory, the water factory will send the water to your home in the pipe, you just turn on the tap, the water will come out. When you turn on the water hose, you’re calling the water plant API.

Calling an API is a pay-as-you-go process; making burgers is the work behind the API.

Ps: It says to explain what an API is for people who don’t know computers.

In computer terms, one way a program can access code written by someone else is through an API.

Such as the program you want to pay treasure payment function, if you want to achieve the function to pay, you have to go to the bank and docking, you can apply for to pay licence, then write program, a string, you can set up a financial company, but now you don’t want to set up a financial company you want to pay by alipay function, how to do? You can use the interface of Alipay, when you call the payment function in your program, you actually request the payment function of Alipay. You don’t have to consider how the payment function of Alipay is done, and what language is used to achieve it. You just need to request the payment request of Alipay according to the standard. This is the API to call Alipay.

What is REST

REST, or Representational State Transfer (REST), is a software architecture style proposed by Dr. Roy Fielding in his doctoral thesis in 2000. It is a design and development method for network applications, which can reduce the complexity of development and improve the scalability of the system. (Quoted from Baidu Baike)

I puked up. I puked up. I puked up.

  • REST describes a form of interaction between the client and server on the network. REST is not a protocol, but a useful API(RESTful interface).

Why use RESTful constructs

Previous web pages are front-end and back-end integration together, such as JSP before. In the previous PC era, there were no problems, but in recent years, with the rapid development of mobile Internet, there are a lot of front-end frameworks. If the front and back ends are not separated, one of the reasons is that the learning cost is very high, the other is that the code repetition rate is too high, which leads to a lot of repeated things, and the code reuse rate is not high. The reuse rate can be increased by using interfaces, as shown in the figure below

Why not use a normal interface, like if you want to get the weather of a city from your website, which you might have done before

https://xxx/getWeather?city= shenzhen

You have to pass two parameters to the background, and it looks bloated.

Designing interfaces in a RESTful way you might design them this way

Shenzhen GET https://xxx/weathers/

We just need to pass a single parameter to the background, which looks pretty neat, and we use a noun in our URI, not a verb. Redirect resources through HTTP verbs. The specific implementation is described below.

That’s why we use RESTful API constructs.

How to design a RESTful API structure system?

  1. Uris are nouns and not verbs. Plural is recommended. HTTP verbs are used to redirect resources.

    • The wrong

      /getOrders

      listCitys

      /getWeathers? Shenzhen city =

    • The correct

      GET/Orders /1: Returns the order with order number 1

      POST/Orders: Adds an order

      Delete /orders/1: Deletes an order with order number 1

      PUT/Orders /1: updates the order with order number 1

  2. Ensure that a method does only one thing and does not change the resource state. For example, the following is not allowed

GET /updateOrder? id=1

  1. Use the correct HTTP Status Code to indicate the Status of the returned request. Such as

    {"code":"200"}

This is an example of how to design a simple RESTful API structured system.

Tips:

The front and back data can be transferred using JSON, can be transferred using XML, I still prefer json to be more convenient

For example, if you ask for the temperature of a city, XML returns something like this

<city>   
    <name>shenzhen</name>
    <temperature>26</temperature>
     <code>200</code>
</city>
Copy the code

This is what happens with json returns

{
    "city":
    {
        "name":"Shenzhen"."temperature":"26"."code":200}}Copy the code