当前位置:网站首页>An example illustrates restful API

An example illustrates restful API

2022-06-24 21:03:00 Messenger Xiaolin

1.Restful API

In a word, it means comply with REST Style API, It's through HTTP It's a simple verb (GET\POST\PUT\DELETE) To execute resources . So what is REST Style , When I saw the words (REpresentational State Trasfer, Presentation layer state transition ) The meaning is very obscure , I will remember the following with examples .

When Xiaochong wants to see the salary comparison of other colleagues , Do the following .
1. A web page for entering company employee information , Search element keywords “ Xiaozhi ”
2. Click on the personal information page of Xiaozhi's colleagues
3. Click salary details to view specific salary information

2.RESTful API Design style of

Let's talk about it with examples RESTful API Design style of :

1. resources It is the salary information of Xiaozhi's colleagues , Jump out of the example itself , Resources are anything , Such as images , data , Songs, etc . Resources can be represented by vectors , For example, pictures can be used JPG Format , The text can be used TXT Format , At present, the most popular expression vector is JSON.

2.URI Uniform Resourse identifier, Unified resource location . Through one URI You can only apply to resources . The first step for the above example is to enter a web page to find the information of the entire company's employees .

3. Unified interface style The operation of data is inseparable from CRUD (create,read,update,delete), Corresponding to HTTP Method :GET Access to resources ,POST New resources ( Or update resources ),PUT Update resources ,DELETE Delete resources . Through the above HTTP Method unifies the operation of data .

4. No state That is, all resources can be accessed through URL Direct positioning , Not dependent on other resources or states . It can be seen that , Each step of the above example depends on the previous step , That is, you can't reach it directly and skip to the 3 Step , This is it. A stateful ; If you can do it in one step , Enter... On the website URI Then we go directly to the third step , This is the case No state .

6. route (Endpoint) Express API The specific website of , For example, the path to information about colleagues can be :https://api.example.com/v1/tongshi
https Is a communication protocol ,api.example.com Is the domain name ,v1 It's the version number ,tongshi Representing the resources of colleague information

7. Filtering information (Filtering) Information is huge , To limit the amount of information or specific information , The server in the background (server) The returned results will be filtered according to the front-end parameters .https://api.example.com/v1/tongshi?limit=100
?limit=100: On behalf of the specified return 100 Bar record

3. Why use this style API Well ?

First of all, let's explain API:(APplicantion interface ) Application program interface , It can be understood as a bridge between the front and the back , The front end calls API Make a request to the background , Backstage server After processing the completion request , Can respond to the front end ;

Use RESTful API Why : backstage server While processing data , Can be through a variety of media ( The android mobile phone ,PC The computer , Apple tablet, etc ) Front end incoming of . In order to enable a background to process many different types of front-end data , Then we need to follow a unified style of interaction .

4.Google about Rest API The explanation of

Said so much , Now it is not difficult to understand the following “ official ” Explained

A REST API (also known as RESTful API) is an application programming interface (API or web API) that conforms to the constraints of REST architectural style and allows for interaction with RESTful web services. REST stands for representational state transfer and was created by computer scientist Roy Fielding.

REST API( Also known as RESTful API) Is an application programming interface (API or web API), It conforms to REST Architectural style constraints , Allow and RESTful web Services interact .REST Represents the transfer of a concrete state , By computer scientists Roy Fielding establish .

REST is a set of architectural constraints, not a protocol or a standard. API developers can implement REST in a variety of ways.

REST Is a set of architectural constraints , Not an agreement or standard .API Developers can implement... In many ways REST.

https://www.redhat.com/en/topics/api/what-is-a-rest-api

原网站

版权声明
本文为[Messenger Xiaolin]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211322518487.html