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 usernamesCreate a variable called
new_username with a username to addWrite code to check if
new_username is already in usernamesIf 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