Skip to content

Instantly share code, notes, and snippets.

@IwonGunawan
Last active August 28, 2023 03:34
Show Gist options
  • Save IwonGunawan/3d0976325b1ebd141336d5e598bb7e25 to your computer and use it in GitHub Desktop.
Save IwonGunawan/3d0976325b1ebd141336d5e598bb7e25 to your computer and use it in GitHub Desktop.
Format & Code Response API

1. SCHEMA

{
  "message": string,
  "result": object | Array<object> ,  (optional)
  "pagination":  Object<string>         (optional)
}

2. EXAMPLE

200 (list)

The request succeeded
{
  "message": "success get data",
  “result”: [
      {
        “id”: 1,
        “name”: “Iwon”,
        “age”: 20
      },
      {
        “id”: 2,
        “name”: “Kukuh”,
        “age”: 30
      }
  ],
  "pagination":{
      "total_data":200,
      "total_pages":20,
  }
}

200 (list) => no data

The request succeeded but empty data
{
  "message": "data is empty",
  “result”: []
}

200 (detail or everything else list)

The request succeeded for detail and anything other than a list
{
  "message": "success get data",
  “result”: {
    “id”: 1,
    “name”: “Rifky”,
    “age”: 20
  }
}

400 Bad Request

The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request body, param or query).
{
  "message": "number not found in database",
  “errors”: [
    “62845367890 (Jono) unregistered on row 3”,
    “62847367990 (Joni) unregistered on row 4”
  ]
}

401 Unauthorized

{
  "message": "Incorrect bearer token"
}

403 Forbidden

The client does not have access rights to the content; that is, it is unauthorized, so the server is refusing to give the requested resource. Unlike 401 Unauthorized, the client's identity is known to the server.
{
  "message": "your account not getting access for this request"
}

404 Not Found

The server cannot find the requested resource. In the browser, this means the URL is not recognized.
{
  "message": "page not found"
}

500 Internal Server Error

The server has encountered a situation it does not know how to handle.
{
  "message": "Internal server error"
}

if nothing is defined yet

- Successful responses (200 – 299)
- Client error responses (400 – 499)
- Server error responses (500 – 599)

3. RESPONSE CODE

Success Response

200 (OK)
  method: GET, PUT, POST, DELETE
201 (Created)
  method: POST

Exception Response

Client Errors:
  400 (Bad Request)
    Errors:
      - Incorrect payload/body
      - Incorrect param
      - Incorrect query
  401 (Unauthorized)
    Errors:
      - Incorrect token
      - Incorrect api key
  403 (Forbidden)
    Errors:
      - Account role not gaining access
  404 (Not Found)
    Errors:
      - Incorrect endpoint/url


Server Errors:
  500 (Internal Server Error)
  Errors:
    - All errors that come from the server and are unknown
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment