What is the recommended and the best RESTful API result structure. Thats what i m doing right now:
GET /api/v1/users
{
status: "success",
result: [{...}, ...]
}
What is the recommended and the best RESTful API result structure. Thats what i m doing right now:
GET /api/v1/users
{
status: "success",
result: [{...}, ...]
}
I would go with a different approach (though yours is not wrong in any way but I think is less common):
Let the status be part of HTTP header with an HTTP return code (200, 201, ..., 400, 404, ..., etc.) and in the case you mentioned, an JSON array instead of the result field: [{...}, ...]
A simple example:
Request:
GET /api/v1/users HTTP/1.1
Response:
200 OK
Content-Type: application/json
Date: Sun, 07 Sep 2014 15:24:04 GMT
Content-Length: 261
....
[
{
"username": ...,
"email": ...,
"firstName": ...,
"lastName": ...,
"password": ...,
...
},
{
"username": ...,
"email": ...,
"firstName": ...,
"lastName": ...,
"password": ...,
...
}
]
There are many JSON hypermedia formats, for example:
Your status: "success",
does not meat with the uniform interface / self-descriptive messages REST constraint. You should use HTTP standards instead of creating your own. For further details about REST: Representational state transfer (REST) and Simple Object Access Protocol (SOAP)