admin管理员组文章数量:1125748
app = Flask(__name__)
employees = [
{"id": 1, "Name": "Ram", "Age": 20,"Gender":"Male"},
{"id": 2, "Name": "Sita", "Age": 25,"Gender":"Female" }
]
@app.route('/', methods=['GET'])
def get_employees():
return (employees)
@app.route('/employees/id', methods = ['GET'])
def get_employee(employee_id):
for employee in employees:
if employee['id']== employees_id:
return employee
return {'error':'Employee not found'}
I am new to Flask. I am trying to get employee by ID, but it throws 405 error. However I was able to get all the employees but not according to id.
app = Flask(__name__)
employees = [
{"id": 1, "Name": "Ram", "Age": 20,"Gender":"Male"},
{"id": 2, "Name": "Sita", "Age": 25,"Gender":"Female" }
]
@app.route('/', methods=['GET'])
def get_employees():
return (employees)
@app.route('/employees/id', methods = ['GET'])
def get_employee(employee_id):
for employee in employees:
if employee['id']== employees_id:
return employee
return {'error':'Employee not found'}
I am new to Flask. I am trying to get employee by ID, but it throws 405 error. However I was able to get all the employees but not according to id.
Share Improve this question edited 2 days ago SevC_10 3463 silver badges10 bronze badges asked Jan 9 at 3:55 Sushmita ChaurasiaSushmita Chaurasia 132 bronze badges New contributor Sushmita Chaurasia is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 2 |1 Answer
Reset to default 1you are missing the proper format for route passing id which would be <int:employee_id>
as well as in parameter its employee_id
from flask import Flask, jsonify, request
app = Flask(__name__)
employees = [
{"id": 1, "Name": "Ram", "Age": 20,"Gender":"Male"},
{"id": 2, "Name": "Sita", "Age": 25,"Gender":"Female" },
{"id": 3, "Name": "Mahesh", "Age":45,"Gender":"Male"},
{"id": 4, "Name": "Rita", "Age": 52,"Gender":"Female"},
]
@app.route('/', methods=['GET'])
def get_employees():
return (employees)
@app.route('/employees/<int:employee_id>', methods = ['GET'])
def get_employee(employee_id):
for employee in employees:
if employee['id']== employee_id:
return employee
return {'error':'Employee not found'}
@app.route('/', methods=['POST'])
def add_employee():
new_employee={'id':len(employees)+1, 'Name':request.json['Name'], 'Age':request.json['Age'], 'Gender':request.json['Gender']}
employees.append(new_employee)
return(new_employee)
@app.route('/employees/<int:employee_id>', methods=['PUT'])
def update_employee(employee_id):
for employee in employees:
if employee['id']== employee_id:
employee['Name']= request.json['Name']
employee['Age']=request.json['Age']
employee['Gender']=request.json['Gender']
return employee
return {'error':'Employee not found'}
@app.route('/employees/<int:employee_id>', methods=['DELETE'])
def delete_employee(employee_id):
for employee in employees:
if employee['id']== employee_id:
employees.remove(employee)
return {'data':'Employe has been removed'}
return {'error':'Employee not found'}
if __name__== "__main__":
app.run(debug = True).
本文标签: pythonCreating an api in flask without database leads to 405 errorStack Overflow
版权声明:本文标题:python - Creating an api in flask without database leads to 405 error - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736673631a1947058.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
@app.route('/employees/id'
Presumably you intendedid
to be a variable argument, but you have not used the right syntax for this. – John Gordon Commented Jan 9 at 4:21employee_id
, but then it tries to use a variable namedemployees_id
. Are you sure this is your real code? – John Gordon Commented Jan 9 at 4:22