Home > Enterprise >  Naming convention for WebAPI that return boolean Value
Naming convention for WebAPI that return boolean Value

Time:04-14

The practice I usually follow for naming conventions for WebAPI action methods is to name the action method as a NOUN not as VERB.

// GET {actionname} = /products
// GET api/{actionname}/{id} =  /products/5
// POST api/{actionname} =  /products
// PUT api/{actionname}/{id} =  /products/5

Question is what is good approach for naming convention if we are checking or validating some thing Like what should be the name of the method that takes userid as input and validates if the user exists or not?

CodePudding user response:

A pattern I am familiar with for this particular usecase is

//POST /{entity}/{action} = /products/validate

For the example in your question, you can use a get request to /users/{userid}/validate for an endpoint that just checks if a user exist and returns a boolean

CodePudding user response:

WEB API Convention/Practice:

The convention you are following for WEB API is correct, but it should be a plural noun. enter image description here

Based on above convention your method should be like:

GET https://localhost:7132/api/users?id=1 HTTP/1.1


HTTP/1.1 200 OK
Content-Type: application/json

{
    "exist": true
}

Note: However, there are some requirements and scenarios where we need to focus on context and sometimes it restricts us to follow the convention strictly. It becomes fair in those perticular scenarios. At the end we opt to make our customer/client satisfied.

  • Related