【问题标题】:My API on heroku sends JSON as plain text in the response instead of sending the JSON itself我在 heroku 上的 API 在响应中将 JSON 作为纯文本发送,而不是发送 JSON 本身
【发布时间】:2020-10-13 21:59:19
【问题描述】:

我使用flask-RESTful在python中创建了一个REST API,要在本地运行它我可以使用flask并且它工作正常,但是要在heroku上部署我必须使用gunicorn,问题是gunicorn将json作为纯文本发送而且它很丑,我使用 gunicorn 因为我不能单独使用烧瓶,它无法在香草 heroku 上工作,而且没有指南在 heroku 上实际使用没有 gunicorn 的烧瓶。

PS[1]:我没有在flask或gunicorn中指定ip地址,我让它自己决定。

PS[2]:我在 heroku 上使用 gunicorn 的 api 链接:https://arjixgamersapi.herokuapp.com/

Local JSON image

Deployed JSON image

【问题讨论】:

    标签: json python-3.x heroku gunicorn flask-restful


    【解决方案1】:

    为了在 Flask 中发送 Json 格式的响应,我曾经使用 flask 包中的 jsonify() 函数。 “它使用应用程序/json mimetype 的给定参数的 JSON 表示创建一个响应”(https://kite.com/python/docs/flask.jsonify)

    【讨论】:

    • 感谢您的真诚回答,但问题不在于我作为响应发送的内容,问题在于以纯文本形式发送响应的 gunicorn,我已经在我的代码中使用了 jsonify 函数。
    猜你喜欢
    • 1970-01-01
    • 2020-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多