0
0
Rest APIprogramming~15 mins

Problem Details (RFC 7807) format in Rest API - Mini Project: Build & Apply

Choose your learning style9 modes available
Problem Details (RFC 7807) format
📖 Scenario: You are building a REST API that needs to return error information in a clear and standard way.Using the Problem Details format (RFC 7807) helps clients understand what went wrong.
🎯 Goal: Create a JSON object that follows the Problem Details (RFC 7807) format to describe an error response.
📋 What You'll Learn
Create a dictionary called problem_details with keys type, title, status, detail, and instance
Set type to "https://example.com/probs/out-of-credit"
Set title to "You do not have enough credit."
Set status to 403
Set detail to "Your current balance is 30, but that costs 50."
Set instance to "/account/12345/msgs/abc"
Print the problem_details dictionary as a JSON string
💡 Why This Matters
🌍 Real World
APIs use the Problem Details format to send clear error messages to clients, making it easier to handle errors.
💼 Career
Understanding standard error formats is important for backend developers and API designers to build reliable and user-friendly services.
Progress0 / 4 steps
1
Create the Problem Details dictionary
Create a dictionary called problem_details with these exact keys and values: "type": "https://example.com/probs/out-of-credit", "title": "You do not have enough credit.", "status": 403, "detail": "Your current balance is 30, but that costs 50.", and "instance": "/account/12345/msgs/abc".
Rest API
Need a hint?
Remember to use exact keys and values as strings or numbers as shown.
2
Import the json module
Add the line import json at the top of your code to use JSON functions.
Rest API
Need a hint?
Use the import statement exactly as shown.
3
Convert the dictionary to a JSON string
Use json.dumps() to convert the problem_details dictionary to a JSON string and store it in a variable called json_output.
Rest API
Need a hint?
Use json.dumps() exactly with problem_details as the argument.
4
Print the JSON output
Write a print() statement to display the json_output variable.
Rest API
Need a hint?
Use print(json_output) exactly to show the JSON string.