0
0
Rest APIprogramming~15 mins

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

Choose your learning style9 modes available
Error Response Format in REST API
📖 Scenario: You are building a simple REST API that returns error messages in a consistent format when something goes wrong.
🎯 Goal: Create a Python dictionary that represents an error response with a code and message, then print it as JSON.
📋 What You'll Learn
Create a dictionary called error_response with keys code and message
Set code to 404 and message to "Resource not found"
Use the json module to convert the dictionary to a JSON string
Print the JSON string
💡 Why This Matters
🌍 Real World
APIs often need to send error messages in a clear, consistent format so clients can understand what went wrong.
💼 Career
Knowing how to format error responses is important for backend developers and anyone working with web services.
Progress0 / 4 steps
1
Create the error response dictionary
Create a dictionary called error_response with these exact entries: 'code': 404 and 'message': "Resource not found".
Rest API
Need a hint?

Use curly braces {} to create a dictionary with keys and values.

2
Import the json module
Import the json module at the top of your code.
Rest API
Need a hint?

Use import json to work with JSON data.

3
Convert the dictionary to a JSON string
Create a variable called error_json and set it to the JSON string version of error_response using json.dumps().
Rest API
Need a hint?

Use json.dumps() to convert a dictionary to a JSON string.

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

Use print() to show the JSON string on the screen.