In django, when using DRF the response for the list is like this
[
{....},
{....},
...
]
If pagination is used in list API then response is like this
{
count: int,
next: "",
previous: "",
result: [
{....},
{....},
...
]
}
For detail, create, update, partial_update APIs response is like this
{....}
If there are any errors like some field (email) error, then API response is like
{
email: [
“This field is required.”
]
}
So there is one query that I’ve because some senior dev (Who worked mostly in App development with flutter) told me that the response should always have to be the same. That dev showed me the API response example as well which was developed in PHP Laravel. The API was to get a filtered list with the POST request and within the request body the filtering key with values in JSON was sent to the BE server. In django I do the same thing with GET requests and using django_filter the key and values are sent in query parameters of that API.
The response that all the APIs developed by that Laravel dev had the same response like this
{
success: true or false,
message: “Student List/Detail/Etc API”,
data: {Here the required data is being transferred}
}
Also when there are errors then Django returns the status code of 400 or greater and the API response is to be handled in catch from client side. But in these APIs all response code were 200 just success key return true or false.
I am confused that I am doing something wrong using DRF default response because if I handle this it will be more complex work as by using ModelViewset we can create a List and Detail API with just less code.
So I’m here to ask the much experienced guys then 1 dev who worked mostly on Laravel APIs. Is it necessary to give the response same even if it is not required because in my opinion the default DRF response is pretty good.
Kindly share your thoughts over this, any suggestion or any insights by some experienced guys will help.
Thanks