Codit / practical-api-guidelines

Practical guidelines for building & designing APIs with .NET.
MIT License
16 stars 5 forks source link

Do not respond with BadRequest when the request is perfectly fine but the operation is not allowed / possible #100

Closed MassimoC closed 5 years ago

MassimoC commented 5 years ago

This should not result in a BadRequest response.

A BadRequest indicates that something is wrong at the client-level; something is wrong with the request object. This is not the case when you have no inventory left. A NotAllowed statuscode might be more appropriate although I'm not convinced of it either.

This issue is related to this code

var customization = await _coditoRepository.GetCustomizationAsync(id);
if (customization == null)
{
    return NotFound(new ProblemDetailsError(StatusCodes.Status404NotFound));
}
if (customization.InventoryLevel <= 0)
{
    return BadRequest();
}

_Originally posted by @fgheysels

MassimoC commented 5 years ago

on v1/CustomizationController.cs

            if (customization.InventoryLevel <= 0)
            {
                return BadRequest();
            }
MassimoC commented 5 years ago

405 "Method Not Allowed" to be used when a HTTP method is not allowed for a specific resource Few alternatives to 400:

fgheysels commented 5 years ago

I think that, for the case where you cannot sell a car because there is no inventory, a 409 statuscode would be appropriate