open-taffeta
open-taffeta copied to clipboard
What HTTP code should be used
It looks like HTTP 400
are now legacy and wrong practice use for API database response.
API now mostly answer HTTP 2XX
if the request succeed
Indeed it's not a HTTP
error but an error that come from the process that your api have implemented.
Do you have an example where you expect a 200 and receive a 400?
Do you have an example where you expect a 200 and receive a 400?
HTTP 200 On login with non existant user for example
Here is my proposal about responses :
General
Commons response should always be an envelop and provide these basis behavior :
- An
error
string/boolean field if an error occured. Can also be a string/int can describe a company rule error. (e.g. "USER_DISABLED") -
message
string field to provide any extra error information or acknowledgement (e.g. "The user account is disabled.") -
data
field that contains the data requested.
Errors
- The HTTP 4xx and 5xx status code can still be used.
- The
error
field should always be provided in case of an error. This field is the one that should be used because it may offer more granularity information about the error by using company error codes. - The
message
field can also be provided for development and default error message purpose.
Exemples
Errors
{
"error": true
...
}
NB: If "error": false
there is no error
{
"error":"MY_INTERNAL_ERROR"
...
}
Model
Request door info GET /doors/:doorId
{
"data":{
"id":"8cdc7721-421f-4d62-af78-9e04821e5e05",
"name":"my door"
}
}
Model list
Request door list GET /doors
the data
field is a list.
{
"data":[
{
"id":"8cdc7721-421f-4d62-af78-9e04821e5e05",
"name":"my first door"
},
{
"id":"1fe1de88-159c-4546-95a1-13140804d361 ",
"name":"my second door"
}
]
}