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 endpoint
s are defined as different strings. What does endpoint
mean here?
An API endpoint is a point at which an API -- the code that allows two software programs to communicate with each other -- connects with the software program. APIs work by sending requests for information from a web application or web server and receiving a response.
Endpoints Frameworks for Python is integrated with the App Engine standard Python 2.7 runtime environment. Endpoints Frameworks consists of tools, libraries, and capabilities that let you generate APIs and client libraries from an App Engine application.
To create an API in Python with Flask, we have to indicate: the endpoint, the method and the function that should be executed on that endpoint. Let's see an example with an API that simply returns the text “Hello world!”. from flask import Flask, jsonify, request,send_file app = Flask() @app.
The thing is that the add_resource
function registers the routes with the framework using the given endpoint
. If an endpoint
isn't given then Flask-RESTful generates one for you from the class name.
Your case is WorkerAPI
, the endpoint will beworkerapi
for these two methods, better make endpoint
explicit and avoid to have conflicting endpoint names registered.
For what's endpoint, you can refer to this answer for more details.
'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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With