DEV Community

Cory Harkins
Cory Harkins

Posted on

2

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!

API Trace View

How I Cut 22.3 Seconds Off an API Call with Sentry πŸ‘€

Struggling with slow API calls? Dan Mindru walks through how he used Sentry's new Trace View feature to shave off 22.3 seconds from an API call.

Get a practical walkthrough of how to identify bottlenecks, split tasks into multiple parallel tasks, identify slow AI model calls, and more.

Read more β†’

Top comments (0)

Sentry image

See why 4M developers consider Sentry, β€œnot bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay