mortb
mortb

Reputation: 9869

What http status code to return for "invalid state change"

This is a question about best REST practices. Considering the controller below which has a method which is supposed to update an application object.

public ApplicationController : Controller
{
    private readonly ApplicationService _applicationService;
    public ApplicationController(ApplicationService applicationService)
    {
       _applicationService = applicationService;
    }

    [HttpPost]
    public IActionResult Post([FromBodey] Application application)
    {
         var previousApplication = _applicationService.Get(applicationId)
         if(application.State == ApplicationState.Approved
             && apreviousApplcation.State != ApplicationState.Pending)
         {
              return StatusCode(*what code here*, "State have to be pending to goto approved");
         }

         _applicationSerivce.Update(application);
        return Ok();
    }
}

What status code would be best to return if it is not possible to change the state of the application to the new state?

Upvotes: 8

Views: 8901

Answers (1)

Torsten
Torsten

Reputation: 752

Have look here https://martinfowler.com/articles/richardsonMaturityModel.html in the level 3 section

Fowler uses a 409 Conflict header to indicate that something went wrong.

Upvotes: 5

Related Questions