what does endpoint mean in flask-restful

Viewed 7009

I defined some resource called WorkerAPI using flask-restful and the plan is to process POST request from /api/workers/new and GET request from /api/workers/. When using the following code

api.add_resource(WorkerAPI, '/api/workers/new')
api.add_resource(WorkerAPI, '/api/workers/')

I get errors:

AssertionError: View function mapping is overwriting an existing endpoint function: workerapi

Then I tried to use the following, which seems to work, although I don't know why it works.

api.add_resource(WorkerAPI, '/api/workers/new', endpoint='/workers/new')
api.add_resource(WorkerAPI, '/api/workers/', endpoint='/workers/')

It looks like redundant information to me though. It seems the site works as long as the two endpoints are defined as different strings. What does endpoint mean here?

2 Answers

'endpoint' is actually an identifier that is used in determining what logical unit of your code should handle a specific request.

So, an URL path is mapped to an endpoint and then an endpoint is mapped to a view function in your flask app.

In your case, when you are explicitly defining your endpoints for your resources, it helps the flask app internally to map it efficiently to a list of routes, while when your are not explicitly defining it, flask takes some default value, as explained by @Tiny.D, in the above answer.

Related