Error codes help machines understand what went wrong when they talk to a server. They make it easy to fix problems automatically.
Error codes for machine consumption in Rest API
Start learning this pattern below
Jump into concepts and practice - no test required
HTTP/1.1 404 Not Found Content-Type: application/json { "error_code": 404, "message": "Resource not found" }
The first line is the HTTP status line with a numeric code and a short phrase.
The body usually contains a JSON object with details like error_code and message.
HTTP/1.1 200 OK Content-Type: application/json { "status": "success", "data": {...} }
HTTP/1.1 400 Bad Request Content-Type: application/json { "error_code": 400, "message": "Invalid input data" }
HTTP/1.1 401 Unauthorized Content-Type: application/json { "error_code": 401, "message": "Authentication required" }
HTTP/1.1 500 Internal Server Error Content-Type: application/json { "error_code": 500, "message": "Server error, try again later" }
This small web server returns an item name if it exists. If not, it sends a 404 error code with a message in JSON. Machines can read this to know the item was missing.
from flask import Flask, jsonify, request app = Flask(__name__) @app.route('/item/<int:item_id>') def get_item(item_id): items = {1: 'apple', 2: 'banana'} if item_id not in items: return jsonify({"error_code": 404, "message": "Item not found"}), 404 return jsonify({"item_id": item_id, "name": items[item_id]}), 200 if __name__ == '__main__': app.run(debug=True)
Use standard HTTP status codes so machines and developers understand the meaning easily.
Always send error details in JSON for easy parsing by machines.
Keep error messages clear but avoid exposing sensitive information.
Error codes tell machines what happened during a request.
Use HTTP status codes with JSON messages for clear communication.
Proper error codes help automate fixes and improve user experience.
Practice
What is the main purpose of using error codes in a REST API?
Solution
Step 1: Understand the role of error codes
Error codes are used to communicate the result of a request to the client or machine.Step 2: Identify the correct purpose
The main purpose is to inform machines about success or failure of requests, enabling automated handling.Final Answer:
To tell machines what happened during a request -> Option AQuick Check:
Error codes = communicate request status [OK]
- Thinking error codes speed up the API
- Confusing error codes with data storage
- Believing error codes change URLs
Which HTTP status code correctly indicates a successful request in a REST API?
Choose the correct code:
Solution
Step 1: Recall common HTTP status codes
200 means OK (success), 404 means Not Found, 500 means Server Error, 301 means Redirect.Step 2: Identify success code
200 is the standard code for a successful HTTP request.Final Answer:
200 -> Option CQuick Check:
Success code = 200 [OK]
- Choosing 404 thinking it means success
- Confusing 500 with success
- Selecting 301 which is a redirect
Given this JSON error response from a REST API:
{
"status": 404,
"error": "Resource not found"
}What does the 404 status code mean?
Solution
Step 1: Understand HTTP 404 status code meaning
404 means the requested resource was not found on the server.Step 2: Match the error message with the code
The message "Resource not found" confirms the meaning of 404.Final Answer:
The requested resource does not exist -> Option BQuick Check:
404 = resource missing [OK]
- Thinking 404 means success
- Confusing 404 with server error
- Assuming 404 means unauthorized
Look at this REST API error response snippet:
{
"status": 200,
"error": "Invalid input data"
}What is wrong with this error code usage?
Solution
Step 1: Analyze the status code and message
Status 200 means success, but the message says "Invalid input data" which is an error.Step 2: Identify the mismatch
Using 200 with an error message is incorrect; error codes like 400 should be used for invalid input.Final Answer:
Status 200 should not be used with an error message -> Option AQuick Check:
Status 200 = success, not error [OK]
- Ignoring mismatch between status and message
- Thinking 200 can mean error
- Assuming JSON format is wrong
You want to design a REST API that returns error codes machines can easily understand and act on. Which approach is best?
Choose the best practice:
Solution
Step 1: Understand best practices for machine-readable errors
Machines need both status codes and clear JSON messages to understand errors and automate responses.Step 2: Evaluate options
Return HTTP status codes with clear JSON error messages explaining the problem provides both HTTP status codes and JSON messages, which is the recommended approach.Final Answer:
Return HTTP status codes with clear JSON error messages explaining the problem -> Option DQuick Check:
Status + JSON error message = best practice [OK]
- Sending no message body with status codes
- Using success codes for errors
- Returning HTML instead of JSON for errors
