如何通过 django 或 flask 处理 json 格式的 post 请求以在 python 中拥有笔记本电脑

How to handle a post request in json format to own laptop in python via django or flask

我在 thethingsstack 网页 cloud.thethings.network 上配置了一个 http webhook,它向配置的端点(我的笔记本电脑)执行 post 请求,如下所示:

http://xxx.ngrok.io -> http://localhost:80
For uplink message: http://localhost:80/uplink-message
For join ack: http://localhost:80/join-accept

使用 ngrok.exe http 80 我还可以看到我在命令行中收到了 post 请求:

POST /uplink-enabled           502 Bad Gateway                                                                          POST /uplink-enabled           502 Bad Gateway                                                                          POST /uplink-enabled           502 Bad Gateway 

但是,按照在线教程,我无法接收 json 格式的数据: enter link description here enter link description here 有没有连续监听http端口并处理post请求的代码示例?

编辑: 我的烧瓶代码如下所示:

# import main Flask class and request object
from flask import Flask, request

# create the Flask app
app = Flask(__name__)

@app.route('/uplink-enabled')
def query_example():
    return 'Query String Example'

if __name__ == '__main__':
    # run app in debug mode on port 5000
    app.run(debug=True, port=80)

我也能看到传入的 post 请求,但收到错误消息:

  • Debug mode: on * Restarting with stat * Debugger is active! * Debugger PIN: 852-174-427 * Running on http://127.0.0.1:80/ (Press CTRL+C to quit) 127.0.0.1 - - [18/Aug/2021 11:12:46] "POST /uplink-enabled HTTP/1.1" 405 -

我的 ngrok 界面如下所示:

Session Status online
Session Expires 1 hour, 19 minutes
Version 2.3.40
Region United States (us)
Web Interface http://127.0.0.1:4040
Forwarding http://xxx.ngrok.io -> http://localhost:80 Forwarding
https://xxx.ngrok.io -> http://localhost:80

@app.route('/uplink-enabled', methods= ['POST', 'GET'])
def query_example():
    return 'Query String Example'

这将处理 405 错误。

您的开发服务器 returns HTTP 405(方法不允许),因为 路由定义必须显式定义方法:

@app.route('/uplink-enabled', methods=['POST'])