【问题标题】:Python Flask Request None Problem on AWS Elastic Beanstalk [duplicate]AWS Elastic Beanstalk上的Python Flask请求没有问题[重复]
【发布时间】:2021-05-16 22:28:08
【问题描述】:

我有这段代码,它是一个 post 方法(Python Flask),

@application.route('/try', methods=['POST'])
def tryPost() :
    content = "```" + str(request.json) + "```"
    return jsonify(content)

当我在本地运行应用程序并将邮递员上的 json 发布到 http://127.0.0.1:5000/try 时,我可以看到相同的 json 结果。

但是在我将我的应用程序推送到 aws elastic beanstalk 之后,我发布了相同的方法,它返回了 "```None```"

是什么原因?

这是示例 json

{
    "api_app_id": "A02",
    "token": "Shh_its_a_seekrit",
    "container": {
        "type": "message",
        "text": "The contents of the original message where the action originated"
    }
}

【问题讨论】:

    标签: python amazon-web-services flask amazon-elastic-beanstalk


    【解决方案1】:

    request.json 仅在您使用Content-Typeapplication/json 发送请求时有效。否则为None

    没有内容类型,你应该使用request.data

    @application.route('/try', methods=['POST'])
    def tryPost() :
        content = "```" + str(request.data) + "```"
        return content
    

    【讨论】:

    • 谢谢@Marcin,但现在它返回这样,我该如何解决这个问题? ; b'{\r\n\t"api_app_id": "A02",\r\n\t"token": "Shh_its_a_seekrit",\r\n\t"container": {\r\n\t\t "type": "message",\r\n\t\t"text": "发起操作的原始消息的内容"\r\n\t}\r\n}'
    • @ŞevvalKahraman 我认为你必须返回request.data,而不需要str 操作。
    • 但是我不能与其他字符串连接:/
    • @ŞevvalKahraman 对不起,我不知道你想达到什么目的。为什么不将 application/json 的 Content-Type 与您的请求一起传递?
    • 谢谢@Marcin,我想在 slack 上显示这个请求。我返回了那个 str(request.data, "utf-8") 并且它有效。
    猜你喜欢
    • 2020-11-07
    • 2017-12-30
    • 2019-08-11
    • 2014-10-19
    • 2020-12-31
    • 2020-08-02
    • 2022-06-23
    • 2020-03-21
    • 2019-11-22
    相关资源
    最近更新 更多