简体   繁体   中英

Best rest API practice: Whether to include a status or not

for instance let me say I have an API that returns several object in an array that have stati... Just like

[{status:good},...,{status:good}]

The application logic says, this array is 'good' iff each objact in it has the status 'good'. Of course.

So, now I return that array through my API JSON encoded. Should I insert the status directly into the API output, like so

{status:good, array:[{status:good},...,{status:good}]}

or should I do it on the client side.

How to decide such cases while API design? Is there something like a guideline? I mean the first approach is more intuitive, while the second is less expensive.

The most important question is what is your API being used for? By adding an extra "summary" status you are de-normalising the data, which is a huge topic in itself.

If it would help users and a priority is to make your API quick & easy to use then include it, make the API easy-to-use, especially if it can reduce the need for repeat calls.

If a priority is data integrity and heavy data usage over ease-of-use, then don't include a summary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM