TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ask HN: REST API response – Should you envelope or not?

5 点作者 googlycooly超过 4 年前
Can anyone who has years of experience in building REST APIs explain which option is better? To envelop or not?<p>If we envelop, what should be the key? Should it be common for all responses like &quot;data&quot;:[] or specific to resources like &quot;users&quot;:[]

2 条评论

boyter超过 4 年前
I always do. Been working with rest api’s for 10 years. Mostly because front enders and other api consumers I work with like the standard response and because I can give details about the backend. I put everything into a response key but data would work.<p>I put in a few standard fields. Time which is utc to catch caching issues. Messages for useful messages such as DB down fell over to cache or some such. Success which is used to indicate if everything worked. Note I use http codes for failures but say something fell over to a backup this would be set to false. I also have timing which contains the time for backend calls to respond or other such processing times. This is helpful when someone says a call is slow, just view the response and you get an idea where the issue might be. Has saved me a few times when integrating with 3rd parties and allows for finger pointing that’s not at me.<p>An example below.<p><pre><code> { &quot;success&quot;: true, &quot;messages&quot;: [], &quot;time&quot;: &quot;2021-01-26T07:49:28.635248481Z&quot;, &quot;timing&quot;: [ { &quot;timeMillis&quot;: 0, &quot;source&quot;: &quot;HealthCheck&quot; } ], &quot;response&quot;: { &quot;memUsage&quot;: &quot;memoryusage::Alloc = 7025 MB::TotalAlloc = 82490487 MB::Sys = 17503 MB::tNumGC = 31354&quot; } }</code></pre>
smt88超过 4 年前
Envelope. It allows you to add metadata if you want (e.g. request ID, cache date, response time, paging info, or anything else that&#x27;s relevant).