Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST : return 'method not allowed' or not do the method?

Tags:

rest

symfony

I'm building a restful API for my Symfony app.

I have a resource that can't be created via the API, so my first idea was to don't create the POST method/URI. But now I'm wondering if create the POST method and throw manually a 405 Method Not Allowed is not a better idea.

Has anybody an idea of the best solution?

like image 809
Maxime Picard Avatar asked Jul 09 '26 10:07

Maxime Picard


1 Answers

In other frameworks - e.g. spring in java or django rest framework in python - it's done automatically. If an HTTP method isn't listed the framework will automatically return a 405 Method Not Allowed. It's very useful.

Personally I'd go for handling it manually - I mean returning a 405 Method Not Allowed - to be explicit about the error. This is not the case that route does not exist, hence 404 Not Found is a bad choice. It does exist, however not serving POST.

Even 403 Forbidden will be much better than 404 Not Found.

like image 110
Opal Avatar answered Jul 12 '26 06:07

Opal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!