简体   繁体   中英

Flask-RESTful getting 404 for endpoint if I don't put url parameter at the end

My endpoints:

# Routes
service.add_resource(UserRegister, "/register") # Works
service.add_resource(User, "/users/<int:user_id>") # Works
service.add_resource(CategoryListResource, "users/<int:user_id>/categories") # 404 not found
service.add_resource(CategoryResource, "/categories/<int:category_id>") # Works
service.add_resource(TaskListResource, "categories/<int:category_id>/tasks") # 404 not found
service.add_resource(TaskResource, "/tasks/<int:task_id>") # Works

My models are structured so that each user has some categories and each category has some tasks that belong to it. So I wanted my API to reflect that. When I want to GET all categories belonging to a user with user_id = 3 I'd type service/users/3/categories , however this gives me a 404 not found:

"GET /service/users/3/categories HTTP/1.1" 404 -

But when I change my endpoint to for example:

service.add_resource(CategoryListResource, "example/<int:user_id>") # Works fine now

This will work just fine, but it's not what I want, as it doesn't express the logic I mentioned earlier. Is there a way to have url parameters appear at the middle of an endpoint using Flask-RESTful or do I have to settle for a different approach?

PS service is the url_prefix of the Blueprint and it's not causing any problems, in fact all endpoints that have url parameters only at the end work just fine.

尝试将users/<int:user_id>/categories更改为/users/<int:user_id>/categories

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM