Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the good design methods of RESTfulAPI

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

What are the good design methods of RESTfulAPI? I believe many inexperienced people don't know what to do about it. Therefore, this paper summarizes the causes and solutions of the problems. Through this article, I hope you can solve this problem.

Web API has become an important topic in recent years, and a clean API design is very important for back-end systems.

Usually we use RESTful design for Web API, the REST concept separates API structure and logical resources, and manipulates resources through Http methods GET, DELETE, POST, and PUT.

1. Use nouns instead of verbs

Resource

Resources

GET

Read POST

Create PUT

Modify DELETE/cars to return cars collection to create new resource batch update cars delete all cars/cars/711 return specific car this method does not allow updating a specified resource to be good at a specified resource

Do not use:

/ getAllCars

/ createNewCar

/ deleteAllRedCars

2.Get methods and query parameters should not involve state changes

Use the PUT, POST, and DELETE methods instead of the GET method to change the state, and do not use GET to change the state:

GET / users/711?activate

GET / users/711/activate

3. Use plural nouns

Don't confuse singular and plural nouns. To keep it simple, use the plural only for all resources.

/ cars instead of / car

/ users instead of / user

/ products instead of / product

/ settings and deploy / setting

4. Express relationships using sub-resources

If one resource is related to another resource, use child resources:

GET / cars/711/drivers/ returns all drivers of car 711

GET / cars/711/drivers/4 driver number 4 returning to car 711

5. Declare the serialized format using the Http header

On both the client and the server, both parties need to know the format of the communication, which is specified in HTTP-Header

Content-Type defines the request format

Accept defines a series of acceptable response formats

6. Use HATEOAS

Hypermedia as the Engine of Application State hypermedia serves as the engine of application status, and hypertext links can create better text browsing:

{

Id: 711

"manufacturer": "bmw"

"model": "X5"

"seats": 5

"drivers": [

{

"id": "23"

"name": "Stefan Jauker"

"links": [

{

"rel": "self"

"href": "/ api/v1/drivers/23"

}

]

}

]

}

Notice that href points to the next URL

7. Provide functions such as filtering, sorting, selection and paging for collections

Filtering filtering:

Filter with unique query parameters:

GET / cars?color=red returns a red cars

GET / cars?seats

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report