0
0
Rest APIprogramming~15 mins

Error response structure in Rest API - Mini Project: Build & Apply

Choose your learning style9 modes available
Error Response Structure in REST API
📖 Scenario: You are building a REST API that needs to send clear error messages to clients when something goes wrong. This helps users understand what happened and how to fix it.
🎯 Goal: Create a simple error response structure in JSON format that includes an error code, a message, and details.
📋 What You'll Learn
Create a dictionary called error_response with keys code, message, and details
Set code to the integer 404
Set message to the string "Resource not found"
Set details to a list containing the string "The requested item does not exist"
Print the error_response dictionary as a JSON string
💡 Why This Matters
🌍 Real World
APIs need to send clear error messages so users and developers understand what went wrong and how to fix it.
💼 Career
Knowing how to structure and send error responses is important for backend developers and API designers.
Progress0 / 4 steps
1
Create the error response dictionary
Create a dictionary called error_response with these exact entries: code set to 404, message set to "Resource not found", and details set to a list containing "The requested item does not exist".
Rest API
Need a hint?

Use curly braces {} to create a dictionary. The details value should be a list with one string inside.

2
Import the JSON module
Import the json module to convert the dictionary to a JSON string.
Rest API
Need a hint?

Use import json at the top of your code.

3
Convert the dictionary to a JSON string
Use json.dumps() to convert the error_response dictionary to a JSON string and store it in a variable called json_error.
Rest API
Need a hint?

Use json.dumps(error_response) to convert the dictionary.

4
Print the JSON error response
Print the variable json_error to display the JSON string of the error response.
Rest API
Need a hint?

Use print(json_error) to show the JSON string.