1

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.

4
  • Try changing users/<int:user_id>/categories to /users/<int:user_id>/categories Commented Jan 5, 2020 at 15:13
  • @n1rna, thanks a bunch. I hadn't noticed that the / was missing at the beginning. Commented Jan 5, 2020 at 15:16
  • Please mark this question as answered. Commented Jan 5, 2020 at 15:20
  • @n1rna please type your comment as an answer so that I could accept it, I don't think it's possible to mark it as answered otherwise. Commented Jan 5, 2020 at 15:47

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.