DEV Community

Cory Harkins
Cory Harkins

Posted on

Are you using the correct rest attributes?

Hello πŸ‘‹,

Way back when in my early days of being a programmer, when working in full framework MVC, controller endpoints were just a means to an end.

All the things were handled in house by our own systems so it wasn't necessarily crucial to provide the most accurate endpoints, just so long as the job got done we were all happy.

That being said, everything was a [HttpGet] or an [HttpPost].

Needed data? Use a get.
Creating data? Use a post.
Updating? Post.
Delete? Post.

You get the picture. Gets and Posts everywhere all day long.

But as I progressed in my career and curiosity I began to use other controller attributes such as Put and Delete.

Not only did these help, at a glance, to see which method did what in spite of, good or bad, naming conventions; it also made consumption of these endpoints more understandable to front end engineers and external third parties.

The added benefit of using these attributes is that no other targeted rest request type will be allowed to hit that endpoint.

That being said I think it's crucial to know these rest request attributes.

Http Get

Attribute Usage: [HttpGet]
When To Use: When retrieving a collection of data, or a single item of data from some type of storage.

Http Post

Attribute Usage: [HttpPost]
When To Use: When creating a new item to be added to a stored data location.

Http Patch

Attribute Usage: [HttpPatch]
When To Use: When you want to override or update parts of some existing data or object.

Http Put

Attribute Usage: [HttpPut]
When To Use: When you want to completely override or change an entire piece of data or object.

Http Delete

Attribute Usage: [HttpDelete]
When To Use: When you want to delete an object.

These may seem straight forward, but for me, Put vs Patch has always been one that I need to look up from time to time as they're used less often.

I see misuse of these attributes (the biggest offender being Post to do all udpates, deletes, and creates.

And no, the world won't collapse if you use Get and Post over the other options but; we should all strive to build better together and in the evermore connected world, we must make our intentions as clear as we can with our tools.

Thanks everyone!

Top comments (0)