Reputation: 714
I wanted to know which practice to be used for writing the REST API response:-
{
"Headline": {
"score": 10,
"summary": "Public Url Description.",
"status": 1,
"description": "Perfect! The URL contains your name"
},
"Profile Picture": {
"score": 10,
"summary": "Profile Picture Description.",
"status": 1,
"description": "Amazing Job, Now you are more approachable."
}
}
OR
{
"status": 200,
"response": {
"Headline": {
"score": 10,
"summary": "Public Url Description.",
"status": 1,
"description": "Perfect! The URL contains your name"
},
"Profile Picture": {
"score": 10,
"summary": "Profile Picture Description.",
"status": 1,
"description": "Amazing Job, Now you are more approachable."
}
}
}
I am new in writing APIs. I want to know which practice will help me write better APIs.
I am using CakePHP as a framework.
Should I add response code in API response?
Upvotes: 0
Views: 1050
Reputation: 6503
I'd add to @Lutz's answer that there isn't a standard across the industry when returning JSON data via a RESTful API.
However, some people are trying to standardize it.
See http://jsonapi.org/ for more details on this.
Upvotes: 1
Reputation:
The second response duplicates information that is already in the status of the HTTP Response. The HTTP status will be 200 OK
or any other HTTP status code set by the server. Duplicating it in the response body only leads to confusion if you are not carefull to keep both values equal.
Use the first form.
Upvotes: 6