0

The response text from my API call shows in the logs but how can I display one of the values from the output. For example the below returns the result.

{"uuid":"FqDSDSVJY+GYAHSTSGA=","id":6238236373}

I am wanting to render "6238236373" on my index.html page. What is the best option to convert to valid json objects and then call these objects onto my page?

app.py

@app.route('/alert', methods=['GET', 'POST'])
def alert():
    form = ReusableForm(request.form)
     
    if request.method == 'GET': 
    
        import requests

        url = "https://xxxxxxxxxxxxxxxxxxxxx.com/api/v1"

        payload="{}"
        headers = {
        'Content-Type': 'application/json',
        'Accept': 'application/json, application/xml',
        'Authorization': 'Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
        }

        response = requests.request("POST", url, headers=headers, data=payload)

        print(response.text)
    
    return render_template('index.html', form=form,)

index.html

<html>
<body>

{ render id response }

</body>
</html>
davidism
  • 121,510
  • 29
  • 395
  • 339
Luke To
  • 103
  • 6

1 Answers1

1

Given the fact that you're sending and receiving data in json format, I think you could use json to transform the result.text into a dictionary.

import json
#...
data = json.loads(response.text)
#...
print(data['id']) # should output 6238236373

More info about json here.

pazitos10
  • 1,641
  • 16
  • 25