【问题标题】:how to display contents of json object via make_response如何通过 make_response 显示 json 对象的内容
【发布时间】:2021-08-02 13:05:23
【问题描述】:

我将一个 json 对象发送到 web 服务,我正在尝试按如下方式获取该 json 对象:

@app.route("/insecticidesSpecifications/<string:parameters>", methods=['GET'] )
def insecticidesSpecifications(parameters):
    logger.debug(make_response(parameters,200).get_data)
   

结果如下图。我看不到 json 对象的内容。 请告诉我如何正确获取 json 对象的内容。

输出

 INFO:werkzeug: - - [02/Aug/2021 14:59:16] "OPTIONS /insecticidesSpecifications/[object%20Object] HTTP/1.1" 200 -
DEBUG:root:<bound method BaseResponse.get_data of <Response 15 bytes [200 OK]>>
INFO:werkzeug: - - [02/Aug/2021 14:59:16] "GET /insecticidesSpecifications/[object%20Object] HTTP/1.1" 200 -

【问题讨论】:

    标签: python json flask


    【解决方案1】:

    get_data 是一个方法,你需要调用它:

    @app.route("/insecticidesSpecifications/<string:parameters>", methods=['GET'] )
    def insecticidesSpecifications(parameters):
        response = make_response(parameters,200)
        logger.debug(response.get_data())
    

    response变量的使用不是必须的,但是这样更容易阅读

    【讨论】:

    • 我按照你的建议使用了它,但我得到的结果是:>
    • 您仍然没有在get_data 之后包含()。我用参数asdf 调用。使用response.get_data(),结果为b'asdf'。使用response.get_data,结果为&lt;bound method Response.get_data of &lt;Response 4 bytes [200 OK]&gt;&gt;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-14
    • 1970-01-01
    • 1970-01-01
    • 2018-05-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多