Background
We are building a Restful API that should return data objects as JSON. In most of the cases it fine just to return the data object, but in some cases, f.ex. pagination or validation, we need to add some metadata to the response.
What we have so far
We have wrapped all json responses like this example:
{
"metadata" :{
"status": 200|500,
"msg": "Some message here",
"next": "http://api.domain.com/users/10/20"
...
},
"data" :{
"id": 1001,
"name": "Bob"
}
}
Pros
- We can add helpful metadata to the response
Cons
- In most cases we don't need the metadata field, and it adds complexity to the json format
- Since it's not a data object any more, but more like a enveloped response, we can not use the response right away in f.ex backbone.js without extracting the data object.
Question
What is the best practices to add metadata to a json response?
UPDATE
What I've got so far from answers below:
- Remove the
metadata.status
an return the http response code in the http protocol instead (200, 500 ...) - Add error msg to body of an http 500 repsonse
- For pagination i natural to have some metadata telling about the pagination structure, and the data nested in that structure
- Small amount of meta data can be added to http header (X-something)