Designing a Response System using Common HTTP Response Codes for RESTful APIs ( JSON Response Examples Included)

reponse code 2xx, 3xx, 4xx, 5xx

·

4 min read

Successes (2xx):

  • 200 (OK): The request was successful and the response contains the requested data.

  • 201 (Created): A new resource was created successfully. The response typically includes the location of the new resource in the Location header.

  • 202 (Accepted): The request has been accepted for processing, but the processing has not been completed yet.

  • 204 (No Content): The request was successful, but there is no content to send in the response body.

Errors (4xx):

  • 400 (Bad Request): The request is invalid due to syntax errors or missing required data.

  • 401 (Unauthorized): The client is not authorized to access the requested resource.

  • 403 (Forbidden): The client is authorized to access the resource, but the request is forbidden.

  • 404 (Not Found): The requested resource could not be found.

  • 405 (Method Not Allowed): The requested HTTP method is not supported for the resource.

  • 409 (Conflict): The request could not be completed due to a conflict, such as trying to create a resource that already exists.

Others:

  • 301 (Moved Permanently): The requested resource has been permanently moved to a new location.

  • 302 (Found): The requested resource can be found at a new location.

  • 304 (Not Modified): The requested resource has not been modified since the last time it was accessed.

  • 415 (Unsupported Media Type): The request format is not supported by the server.

  • 500 (Internal Server Error): An unexpected error occurred on the server.

This is not an exhaustive list, but it covers some of the most common HTTP response codes used in RESTful APIs.

Remember, the specific code you use will depend on the specific situation and the desired behavior of your API.

Example JSON Responses for Common HTTP Codes:

Success (200 OK):

{
  "message": "Successfully retrieved user data",
  "data": {
    "id": 1,
    "name": "John Doe",
    "email": "john.doe@example.com"
  }
}

Success (201 Created):

{
  "message": "file created successfully",
  "location": "/path/something.jpg",
  "data": {...}
}

Success (204 No Content):

{}

While a 204 No Content response doesn't typically include a body due to its success nature indicating no content to send, you can technically have an empty JSON object{}as the response body. However, it's generally considered unnecessary and most frameworks will handle this automatically

301 Moved Permanently:

{
  "message": "Resource has been moved permanently",
  "location": "/api/v2/users/1"
}

302 Found:

{
  "message": "Resource found at a new location",
  "location": "/api/users/1?new_param=true"
}

415 Unsupported Media Type:

{
  "error": "Unsupported Media Type",
  "message": "The request body format is not supported (e.g., JSON expected, received XML)."
}

429 Too Many Requests:

{
  "error": "Too Many Requests",
  "message": "The client has exceeded the allowed number of requests in a specified time frame."
}

Error (400 Bad Request):

{
  "error": "Missing required field: email",
  "message": "The request is missing the required email field."
}

Error (401 Unauthorized):

{
  "error": "Unauthorized",
  "message": "Invalid credentials provided."
}

Error (403 Forbidden):

{
  "error": "Forbidden",
  "message": "You do not have permission to access this resource."
}

Error (404 Not Found):

{
  "error": "Not Found",
  "message": "The requested resource could not be found."
}

Sure, here are examples for response codes 405 and 409:

405 Method Not Allowed:

{
  "error": "Method Not Allowed",
  "message": "The HTTP method 'PUT' is not allowed for this resource. Please use 'POST' instead."
}

409 Conflict:

This code signifies that the request could not be completed due to a conflict. This typically happens when trying to create a resource that already exists or when modifying a resource in a way that violates constraints.

{
  "error": "Conflict",
  "message": "Resource with ID '123' already exists. Please choose a different ID."
}

These examples showcase the structure you can use to convey the issue and potential solutions in your JSON response for these specific status codes.

Error (500 Internal Server Error):

{
  "error": "Internal Server Error",
  "message": "An unexpected error occurred on the server."
  "code": "3011-ERROR-DB"
}

the "Code": "3011-ERROR-DB" here you can replace with your own BUSINESS_ERROR_BACKEND_CODE. e.g. database error, resource error, file error or anything.

These are just a few examples, and the specific content of your JSON response will vary depending on your specific API and the nature of the request. However, they provide a good starting point for understanding how to format your responses for different situations.

Did you find this article valuable?

Support Rendy S. by becoming a sponsor. Any amount is appreciated!