ulrich201
ulrich201

Reputation: 85

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?

P.S. 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.

Upvotes: 1

Views: 466

Answers (1)

nima
nima

Reputation: 1645

Try changing users/<int:user_id>/categories to /users/<int:user_id>/categories

Upvotes: 1

Related Questions