2

I am new in web api and I am not also a very advanced developer. I've seen an article that shows how we can use multiple http verbs for a single action.

So I like to know that the approach is good. Otherwise then, what can be worse when someone uses multiple http verbs for a single action?

Code taken from this area https://www.codeproject.com/Articles/1005485/RESTful-Day-sharp-Security-in-Web-APIs-Basic#_Toc423441907

[GET("allproducts")]
[GET("all")]
public HttpResponseMessage Get()
{ }

[GET("productid/{id?}")]
[GET("particularproduct/{id?}")]
[GET("myproduct/{id:range(1, 3)}")]
public HttpResponseMessage Get(int id)
{ }

[POST("Create")]
[POST("Register")]
public int Post([FromBody] ProductEntity productEntity)
{ }     

[PUT("Update/productid/{id}")]
[PUT("Modify/productid/{id}")]
public bool Put(int id, [FromBody] ProductEntity productEntity)
{ }

// DELETE api/product/5
[DELETE("remove/productid/{id}")]
[DELETE("clear/productid/{id}")]
[PUT("delete/productid/{id}")]
public bool Delete(int id)
{ }

1) In what kind of scenario people use multiple http verbs for a single action because they create multiple routes for a single action that can confuse people ....... should approach below is good or not ?

[GET("productid/{id?}")]
[GET("particularproduct/{id?}")]
[GET("myproduct/{id:range(1, 3)}")]
public HttpResponseMessage Get(int id)
{}

2)PUT and DELETE is different verbs. how one can use different verbs for single action....is it right approach.

[DELETE("remove/productid/{id}")]
[DELETE("clear/productid/{id}")]
[PUT("delete/productid/{id}")]
public bool Delete(int id)
{
 if (id > 0)
     return _productServices.DeleteProduct(id);
 return false;
}

please clarify the pros and corns of the above code and approach in details. thanks

Monojit Sarkar
  • 2,353
  • 8
  • 43
  • 94
  • 1) There are not multiple verbs (they are all the same) but multiple routes 2) It depends on the developer and/or the contract for the API. It can be right or wrong – Sir Rufo Jun 09 '17 at 09:03
  • The main point of the article is to show you what is possible. You, as the developer, have to decide to use it or not. – Sir Rufo Jun 09 '17 at 09:07
  • @SirRufo see they use multiple http verb DELETE and PUT used for single action called Delete – Monojit Sarkar Jun 09 '17 at 09:48

1 Answers1

0
  1. This is more to do with the design of your application, and how you like your app to be routed. For example, particularproduct/{id?} for getting one of any product, while myproduct/{id:range(1, 3) means getting one of my products, and each person can only have a max of 3 products (that's why there is a range limit).

       [GET("productid/{id?}")]
       [GET("particularproduct/{id?}")]
       [GET("myproduct/{id:range(1, 3)}")]
       public HttpResponseMessage Get(int id){}
    
  2. PUT, DELETE both has side effects, i.e. changes to data. You can even use POST for the Delete(..) action method. In some situations, PUT & DELETE may not be supported (Are the PUT, DELETE, HEAD, etc methods available in most web browsers?), so you have to use POST instead.

So, again, it depends on the design of your app, and having multiple URI's allows you to use it in different scenarios. Most of the time, I just use one URI for each action method.

grepLines
  • 2,478
  • 3
  • 21
  • 32