Hello /g/,
I'm building a REST api using express and mongoose.
For now, my API is returning a simple json object like this with no http status:
{
success: true,
data: [dataarray]
}
or
{
success: false,
error: {errObject}
}
Would it be better to just set different status code to detect errors? So if a request have a 500 status, i'm just returning back the error object, if this is status code 200 i'm sure the data is there so i'm just returning the data array etc...
Any suggestions?
>>58957107
wtf do you want ?
if you just want to know what the error is about just log full object with error info ?
or what is your problem ?
>>58957107{
success: true/false,
data: [stuff or empty if false],
error: int code, 0 no error, ...
}
http://www.restapitutorial.com/httpstatuscodes.html
http://racksburg.com/choosing-an-http-status-code/
Yes use status code, 200 for success, 422 if object couldn't be updated
>>58957275
This
On success{
success: true, // can fairly easily be omitted, success = error == null
error: null,
data: [stuff]
}
On failure{
success: false,
error: { someField: someValue },
data: null
}
>>58957107
yes you are supposed to use http status codes to better express application state
same with http methods, refresh headers etc
don't just post everything, use put or patch to complete or update existing resources, use delete instead of posting to uri/delete
rely on http protocol as much as possible