Reputation: 3748
I'm sure there is probably a duplicate of this question somewhere but I don't know what the right wording is for the question I have.
I have endpoints that I want to reach and I ran curl
commands on them:
curl http://0.0.0.0:5001/job/task/?job_type=medicine&task_type=research
curl http://0.0.0.0:5001/job/task/?job_type=medicine&task_type=research&job_id=123
The errors that I am getting for both are:
<title>404 Not Found</title>
<h1>Not Found</h1>
<p>The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.</p>
A part of my code is shown below, and I have a feeling it has something to do with the way I'm using api.add_resource
:
from flask import Flask
from flask_restful import Resource, Api
app = Flask(__name__)
api = Api(app)
class Test(Resource):
def get(self,job_type,task_type,task_id = None):
if not task_id:
return {"job_type":job_type,"task_type":task_type}
return {"job_type":job_type,"task_type":task_type,"job_id":job_id}
api.add_resource(Test, '/job/task/?<string:job_type>&<string:task_type>', '/job/task/?<string:job_type>&<string:task_type>&<int:job_id>')
How do I fix the error?
Upvotes: 0
Views: 1176
Reputation: 364
Your flask route
api.add_resource(Test, '/job/task/?<string:job_type>&<string:task_type>', '/job/task/?<string:job_type>&<string:task_type>&<int:job_id>')
will translated by flask into
/job/task/?job_type&task_type
not
/job/task/?job_type=xxx&task_type=yyy
That causes endpoint not found error.
Solution
You can access the query string using request.args
(https://flask.palletsprojects.com/en/1.1.x/api/#flask.Request.args)
so, your url definition is should looks like
api.add_resource(Test, '/job/task/')
and in your view
from flask import request
.....
def get(self):
job_type = request.args.get("job_type")
task_type = request.args.get("task_type")
task_id = request.args.get("task_id")
if not task_id:
return {"job_type":job_type,"task_type":task_type}
return {"job_type":job_type,"task_type":task_type,"job_id":job_id}
Another solutions:
Upvotes: 1