【发布时间】:2022-01-27 05:59:39
【问题描述】:
这是一个简单的项目,但不知何故我有货。我正在尝试创建自己的 webhook 接收器服务器,当使用“/webhook”接收到 webhook 时,它会在 index.html 中的“/”处显示接收到的数据。从技术上讲,我只是希望烧瓶使用 '/' 处的 'index.html' 显示在 '/webhook' 收到的最后一个数据,但它不起作用。
这是我的烧瓶目录:
-server.py
-webhook.py
-/templates/index.html
这是我的 server.py 代码:
from flask import Flask, render_template, request, abort, json, redirect, url_for
app = Flask(__name__)
@app.route('/')
def hello():
title = request.args.get('title', None)
jsonfile = request.args.get('jsonfile', None)
print("Recieved:", title, jsonfile, type(jsonfile))
return render_template('index.html', title=title, jsonfile=jsonfile)
@app.route('/webhook', methods=['POST'])
def webhook():
if request.method == 'POST':
data = request.json
return redirect(url_for('hello', title='Result', jsonfile=data)), 'success', 200
else:
abort(400)
if __name__ == '__main__':
app.run()
这是我的 webhook.py:
import requests,json
webhook_url = "http://localhost:5000/webhook"
data = {
'fn_name': 'abc',
'last_name': 'xyz'
}
r = requests.post(
webhook_url, data=json.dumps(data),
headers={
'Content-Type': 'application/json'
}
)
这是我的 index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>{{ title }}</title>
</head>
<body>
<div>
{{ jsonfile }}
</div>
</body>
</html>
我在发送 webhook 时得到这个。
127.0.0.1 - - [26/Jan/2022 12:00:39] "POST /webhook HTTP/1.1" 500 -
和
TypeError: 'int' 对象不可迭代
让我知道你的想法。
【问题讨论】: