'How to read a JSON file in a Heroku application

I have a flask app and I can run it locally with no problem.

I can also run this flask app on Heroku if I remove the first three lines in my app.py and simply return a string (e.g., "Hello").

Therefore, I am mostly sure that the problem lies in the first three lines of webhook function. However, it works fine locally so I am not sure why it fails when I use it in Heroku (perhaps I did not very well understood how Heroku works)

I tested the code locally with insomnia. The structure of my input is a dictionary in insomnia. When I tested locally, request.data has a binary format. This is why I converted it into json file to use json.loads()

The Heroku error is "Internal Server Error"

I will appreciate any suggestion on how to resolve this.

{
"passphrase": "somelongstring123",
"time": "2020-09-05T19:47:00Z",
"ticker": "AAPL",
"bar": {
    "time": "2020-09-05T19:46:00Z",
    "open": 126.35,
    "volume": 12345
},
"strategy": {
    "position_size": 171,
    "order_action": "buy",
    "order_contracts": 171,
    "prev_market_position_size": 1
}}

The structure of my code

folder
-app.py
-Procfile 
-requirements.txt
-other files

app.py code

from flask import Flask, render_template, request
import config, json
import sys

app = Flask(__name__)

@app.route('/')
@app.route('/webhook', methods=['POST'])

def webhook():
    webhook_message = request.data
    my_json = webhook_message.decode('utf8').replace("'", '"')
    webhook_message = json.loads(my_json)
    return str(webhook_message)

Procfile

web: gunicorn app:app

requirements.txt file

flask
gunicorn
requests

Heroku log

   2022-03-06T20:16:12.119835+00:00 app[web.1]:     webhook_message = json.loads(my_json)

   2022-03-06T20:16:12.119836+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/json/__init__.py", line 346, in loads

   2022-03-06T20:16:12.119837+00:00 app[web.1]:     return _default_decoder.decode(s)

   2022-03-06T20:16:12.119837+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/json/decoder.py", line 337, in decode

   2022-03-06T20:16:12.119837+00:00 app[web.1]:     obj, end = self.raw_decode(s, idx=_w(s, 0).end())

   2022-03-06T20:16:12.119838+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/json/decoder.py", line 355, in raw_decode

   2022-03-06T20:16:12.119838+00:00 app[web.1]:     raise JSONDecodeError("Expecting value", s, err.value) from None

   2022-03-06T20:16:12.119838+00:00 app[web.1]: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

   2022-03-06T20:16:12.120371+00:00 app[web.1]: 10.1.32.108 - - [06/Mar/2022:20:16:12 +0000] "GET / HTTP/1.1" 500 290 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:97.0) Gecko/20100101 Firefox/97.0"

   2022-03-06T20:16:12.120462+00:00 heroku[router]: at=info method=GET path="/" host=apitradingview.herokuapp.com request_id=1e375b13-eeec-456b-9154-3935844eac7a fwd="96.74.1.138" dyno=web.1 connect=0ms service=2ms status=500 bytes=463 protocol=https

git repo: https://github.com/HasanOu/trading_apps.git



Solution 1:[1]

You can view the complete traceback of the heroku app: Heroku dashboard -> App page -> Activity -> View Release Log.

There you can see what causes the problem. You can then debug it or if you post it here, people here can be more helpful.

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 appleren