|
|
|
Ask HN: REST API response – Should you envelope or not?
|
|
5 points
by googlycooly
1965 days ago
|
|
Can anyone who has years of experience in building REST APIs explain which option is better? To envelop or not? If we envelop, what should be the key? Should it be common for all responses like "data":[] or specific to resources like "users":[] |
|
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.
An example below.