Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
154 views
in Technique[技术] by (71.8m points)

Rest Api fetch additional data of entity

When we need to fetch a sub resource of an entity, the best practice is to do:

 GET - todos/:id

But I can't find any information about best practices regarding fetching additional data about this resource. What is the best practice if I want to fetch the todos metrics data?

GET - todos/metrics

Is this the correct approach?

If so, it will not collide with todos/:id?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There's no "one right" way to handle this. Your suggestion is appropriate, but can be confused for a potential ID "metrics". You can also use query parameters:

GET - todos/?metrics

or use a placeholder:

GET - todos/-/metrics

or, treat the metrics as a resource of it's own, so you actually are getting metrics, about the todos resource:

GET - metrics/todos

Anything here would be fine, so long as you're consistent across your resources.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...