Index.html not showing data

0
Asked by : melany wijngaard 2020-04-02 13:35

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.

1 Answers

0 votes
0
giselle monteiro
Answer by : giselle monteiro

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))


Related Posts
Related Posts
1
ANGULAR push element in formArray no ren...
نيما جعفری نيما جعفری
2020-02-01  4:27pm 2020-02-01 4:27pm
2
Move multi-select list with JavaScript...
noélie roux noélie roux
2020-03-20  11:41pm 2020-03-20 11:41pm
3
4
How skip unavailable video in Youtube if...
james warren james warren
2021-03-17  11:49am 2021-03-17 11:49am
5
webpage Flickering when scrolling to bot...
dylan soto dylan soto
2021-11-03  6:50pm 2021-11-03 6:50pm