Restful API – JSON response with single or all error(s)

My current project, a restful API, validates a POST request to create a new user and multiple errors could occur (with HTTP status):

  1. username not set (400 BadRequest)
  2. username is taken (409 Conflict)
  3. server can’t establish db connection (500 Internal Server Error)

Should I immediatly send back a JSON response like this

{
    "status": 400,
    "Message": "No username is set"
}

if an error was detected or should is it better if I accumulate all errors like here:

{
    "status": <HTTP STATUS CODE>,
    "errors": [
        {"message": "Username is not set."},
        {"message": "Can't access the database."}
    ]
}

The last approach would not require multiple request to send a valid payload. But which status code should be used, if the username is not set (400 Bad Request) or the server can’t access the database (500 Internal Server Error)?


Source: json

Leave a Reply