0
0
Rest APIprogramming~20 mins

Why consistent formats improve usability in Rest API - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
API Usability Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use consistent response formats in REST APIs?

Imagine you are building a REST API that many developers will use. Why is it important to keep the response format consistent across all endpoints?

AIt helps developers understand and use the API faster because they expect the same structure everywhere.
BIt allows each endpoint to return data in any random format to keep things flexible.
CIt makes the API slower because it has to check the format every time.
DIt forces developers to write more code to handle different formats.
Attempts:
2 left
💡 Hint

Think about how you feel when you use tools that behave the same way every time.

Predict Output
intermediate
1:30remaining
What is the output of this JSON response?

Given this API response format, what will be the value of data['user']['name']?

Rest API
response = {
  "status": "success",
  "data": {
    "user": {
      "id": 101,
      "name": "Alice",
      "email": "alice@example.com"
    }
  }
}

print(response['data']['user']['name'])
A"alice@example.com"
B"Alice"
C101
DKeyError
Attempts:
2 left
💡 Hint

Look carefully at the nested keys to find the name.

🔧 Debug
advanced
2:30remaining
Identify the error in this inconsistent API response

This API sometimes returns a list and sometimes a dictionary for the same endpoint. What error will this cause in client code expecting a consistent format?

Rest API
response1 = {"items": [{"id": 1}, {"id": 2}]}
response2 = {"items": {"id": 1}}

# Client code expects a list:
for item in response1['items']:
    print(item['id'])

for item in response2['items']:
    print(item['id'])
ATypeError because 'int' object is not iterable in the second loop
BKeyError because 'id' does not exist
CNo error, both loops run fine
DSyntaxError due to missing colon
Attempts:
2 left
💡 Hint

Check the type of response2['items'] and how the loop treats it.

📝 Syntax
advanced
1:30remaining
Which option shows a valid consistent JSON error response format?

Choose the option that correctly represents a consistent JSON error response with a message and code.

A{"error": {"code" => 404, "message" => "Not Found"}}
B{"error": ["code": 404, "message": "Not Found"]}
C{"error": {code: 404, message: "Not Found"}}
D{"error": {"code": 404, "message": "Not Found"}}
Attempts:
2 left
💡 Hint

Remember JSON keys and string values must be in double quotes and use colons.

🚀 Application
expert
2:00remaining
How many items are in the combined consistent API response?

You receive two paginated API responses with consistent formats. How many total items are there combined?

Rest API
response_page1 = {"status": "success", "data": {"items": [1, 2, 3], "page": 1}}
response_page2 = {"status": "success", "data": {"items": [4, 5], "page": 2}}

combined_items = response_page1['data']['items'] + response_page2['data']['items']

print(len(combined_items))
A2
B3
C5
DTypeError
Attempts:
2 left
💡 Hint

Count the total number of items in both lists combined.