ASP.NET Core Web API Interview Question #21

When should a Web API return 409 Conflict?

Web API & REST Fundamentals, Controllers, HTTP Methods & Status Codes Junior Beginner

Quick Interview Answer

409 Conflict indicates that the request cannot be completed because it conflicts with the current state of the target resource or system. Examples include certain duplicate-key situations, concurrency conflicts, or state transitions that collide with existing state.

Detailed Explanation

409 Conflict indicates that the request cannot be completed because it conflicts with the current state of the target resource or system. Examples include certain duplicate-key situations, concurrency conflicts, or state transitions that collide with existing state.

The response should provide a safe, useful error representation so the client can understand or resolve the conflict.

Code Example

if (await _service.EmailExistsAsync(request.Email))
{
    return Conflict(new
    {
        error = "A customer with this email already exists."
    });
}