Skip to content
Advertisement

Flask-RESTful custom routes other than GET,PUT,POST,DELETE

In Flask-RESTful we add an api route like the below

JavaScript

so that GET /api/kitty –> to CuteKitty.get() method; like this for all HTTP verbs

Lets say that I need to provide my api consumers with a cute api like

JavaScript

How can i achive the above routing with api.add_resource

JavaScript

Like wise how to add route like /api/kitty/<int:kitty_id>/habits –> CuteKitty.habits(kitty_id)

Advertisement

Answer

Flask-RESTful is designed to implement RESTful APIs specifically by interpreting the HTTP Request method. Drink and Meow are not standard HTTP methods so Flask-RESTful isn’t concerned with the drink and meow methods in the resource.

The solution to this is to define multiple API routes:

JavaScript

The less intuitive (and imo far worse) way is by creating a frankenresource:

JavaScript

And then break the args with split('/') and call task with them. Alternatively, you could set these as URL arguments (/endpoint/?task=drink&what=milk) — which is still a valid RESTful architecture.

You could also subclass the Resource class and implement the desired functionality yourself — in this case, I’d recommend looking at how Flask-Classy implements this. Or you could pick up Flask-Classy and toy with it and see how you like that, too; however, for a straight up API I think RESTful brings a lot more to the table than Classy.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement