烧瓶REST-API响应问题

时间:2020-08-24 06:51:24

标签: json python-3.x rest flask sqlalchemy

我正尝试通过REST-API和JSON发送数据库信息,但是响应出现了问题,当我用Postman测试时,甚至没有出现。没有任何线索这可能是什么,所以什么也不要尝试。这是代码

@app.route('/wells/<int:well_id>', methods=['GET'])
def get_well(well_id):
    session = create_session()
    well = jsonify(session.query(Well, Well.id == well_id).first())
    session.close()
    return json.dumps(well)

2 个答案:

答案 0 :(得分:0)

首先,您的代码看起来有效。
在这种情况下,请添加一个try和except块,以查看代码中是否存在任何异常。

@app.route('/wells/<int:well_id>', methods=['GET'])
def get_well(well_id):
    try:
        session = create_session()
        well = jsonify(session.query(Well, Well.id == well_id).first())
    except Exception as e:
        #this will tell you if there are anything wrong
        print(e)
        #hope this help your api return something
        well = {'error':e}
    finally:
        #better to close your connection even error
        session.close()
    return json.dumps(well)

此外,如果输入正确,请再次检查邮递员的网址。

答案 1 :(得分:0)

您可以通过这种方式完成

[2], [-2]