Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct HTTP verb to use for a business rules validation endpoint in Web API

I have the below Web API controller. Its sole responsibility is to validate the incoming document against a set of business rules and return a result. Which is the correct HTTP verb to use for this controller action?

//[Http<???>]
public IActionResult ValidateBusinessRules([FromBody BusinessDocument document)
{
  var result = ValidateBusinessRules(document);
  return Ok(result);
}
like image 565
Ershad Nozari Avatar asked Oct 14 '25 05:10

Ershad Nozari


1 Answers

[FromBody] explicitly tells the model binder to check the request body for data to bind. And since only certain request allow a body then it means that it works with POST or PUT.

POST would be the default verb to use in this scenario. Taking model state into consideration the action can look like this

[HttpPost]
public IActionResult ValidateBusinessRules([FromBody] BusinessDocument document) {
    if(ModelState.IsValid) {
        var result = ValidateBusinessRules(document);            
        if(result.IsValid) { //assuming result has a flag
            return Ok(result);
        }
        return BadRequest(result);//assuming result provides relevant details.
    }
    return BadRequest(ModelState);
}

That way the status of the response can provide some relevant feedback about the request made.

like image 182
Nkosi Avatar answered Oct 17 '25 20:10

Nkosi