0
0
Rest APIprogramming~15 mins

409 Conflict in Rest API - Mini Project: Build & Apply

Choose your learning style9 modes available
Handling 409 Conflict in REST API
📖 Scenario: You are building a simple REST API for managing a list of usernames. The API should prevent adding duplicate usernames.
🎯 Goal: Create a REST API endpoint that adds a username to a list. If the username already exists, the API should respond with a 409 Conflict status code and a clear message.
📋 What You'll Learn
Create a list called usernames with initial usernames
Create a variable called new_username with a username to add
Write code to check if new_username is already in usernames
If it exists, respond with status code 409 and message 'Username already exists'
If it does not exist, add new_username to usernames and respond with status code 201 and message 'Username added'
💡 Why This Matters
🌍 Real World
APIs often need to prevent duplicate entries like usernames or emails. Returning a 409 Conflict status helps clients understand the problem clearly.
💼 Career
Backend developers and API designers must handle conflicts gracefully to build reliable and user-friendly services.
Progress0 / 4 steps
1
Create initial usernames list
Create a list called usernames with these exact values: 'alice', 'bob', 'charlie'
Rest API
Need a hint?

Use square brackets to create a list and separate items with commas.

2
Set the new username to add
Create a variable called new_username and set it to the string 'bob'
Rest API
Need a hint?

Assign the string 'bob' to the variable new_username.

3
Check for duplicate and prepare response
Write an if statement to check if new_username is in usernames. If yes, create a variable status_code with value 409 and a variable message with value 'Username already exists'. Otherwise, add new_username to usernames, set status_code to 201, and message to 'Username added'.
Rest API
Need a hint?

Use if new_username in usernames: to check duplicates and append() to add new usernames.

4
Print the API response
Print the status_code and message variables in this exact format: Status: <status_code>, Message: <message>
Rest API
Need a hint?

Use an f-string to format the print output exactly as shown.