'index.html not showing data
This is a simple project but I am stock somehow. I am trying to create my own webhooks receiver server that shows received data in index.html at '/' when a webhook is received using '/webhook'. Technically, I just want the flask to show the last data received at '/webhook' using 'index.html' at '/' but it doesn't work.
Here's my flask directory:
-server.py
-webhook.py
-/templates/index.html
Here's my server.py code:
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()
Here's my 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'
}
)
And here's my index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>{{ title }}</title>
</head>
<body>
<div>
{{ jsonfile }}
</div>
</body>
</html>
I get this when I send webhook.
127.0.0.1 - - [26/Jan/2022 12:00:39] "POST /webhook HTTP/1.1" 500 -
and
TypeError: 'int' object is not iterable
Let me know what you think.
Solution 1:[1]
You placed these outside of the function, so it was trying to return all three and didn't know what to do with the "200":
return redirect(url_for('hello', title='Result', jsonfile=data)), 'success', 200
Not sure if "success" and "200" are valid arguments to the redirect function but the desired result can be achieved with:
return redirect(url_for('hello', title='Result', jsonfile=data))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ying n Yang |
