Reputation: 123
When I try to host a Flask API on Heroku, I don't get any errors (it even says "Build succeeded") on the deploy logs, but when I try to execute a POST request, I simply get the HTML of the page instead of the JSON data.
It works fine on localhost, it problem only occurs on Heroku. Since the Server is in a folder and not at the root of the repository, I had to create a branch (called it Heroku) that only contains the server files.
Is it a problem with the ports?
The route I'm trying to access:
@app.route('/api/model', methods = ["POST"])
def model():
try:
data = request.get_json()
print(f"Received model: {data}")
# `model_func()` is a function that returns a dictionary
return jsonify(model_func(data))
except:
print("Model didn't work")
if __name__ == "__main__":
app.run(host='localhost', port=os.environ.get('PORT'))
How I am accessing it (Postman):
Heroku Logs:
-----> Building on the Heroku-20 stack
-----> Using buildpack: heroku/python
-----> Python app detected
-----> No Python version was specified. Using the same version as the last build: python-3.9.5
To use a different version, see: https://devcenter.heroku.com/articles/python-runtimes
-----> Requirements file has been changed, clearing cached dependencies
-----> Installing python-3.9.5
-----> Installing pip 20.2.4, setuptools 47.1.1 and wheel 0.36.2
-----> Installing SQLite3
-----> Installing requirements with pip
Collecting Flask==2.0.0
Downloading Flask-2.0.0-py3-none-any.whl (93 kB)
Collecting Flask-Cors==3.0.10
Downloading Flask_Cors-3.0.10-py2.py3-none-any.whl (14 kB)
Collecting requests==2.22.0
Downloading requests-2.22.0-py2.py3-none-any.whl (57 kB)
Collecting eventlet==0.31.0
Downloading eventlet-0.31.0-py2.py3-none-any.whl (224 kB)
Collecting asyncio==3.4.3
Downloading asyncio-3.4.3-py3-none-any.whl (101 kB)
Collecting python-dotenv==0.17.1
Downloading python_dotenv-0.17.1-py2.py3-none-any.whl (18 kB)
Collecting numpy==1.20.3
Downloading numpy-1.20.3-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (15.4 MB)
Collecting scipy==1.6.3
Downloading scipy-1.6.3-cp39-cp39-manylinux1_x86_64.whl (27.3 MB)
Collecting Werkzeug>=2.0
Downloading Werkzeug-2.0.1-py3-none-any.whl (288 kB)
Collecting itsdangerous>=2.0
Downloading itsdangerous-2.0.1-py3-none-any.whl (18 kB)
Collecting click>=7.1.2
Downloading click-8.0.1-py3-none-any.whl (97 kB)
Collecting Jinja2>=3.0
Downloading Jinja2-3.0.1-py3-none-any.whl (133 kB)
Collecting Six
Downloading six-1.16.0-py2.py3-none-any.whl (11 kB)
Collecting certifi>=2017.4.17
Downloading certifi-2021.5.30-py2.py3-none-any.whl (145 kB)
Collecting idna<2.9,>=2.5
Downloading idna-2.8-py2.py3-none-any.whl (58 kB)
Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1
Downloading urllib3-1.25.11-py2.py3-none-any.whl (127 kB)
Collecting chardet<3.1.0,>=3.0.2
Downloading chardet-3.0.4-py2.py3-none-any.whl (133 kB)
Collecting greenlet>=0.3
Downloading greenlet-1.1.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (162 kB)
Collecting dnspython<2.0.0,>=1.15.0
Downloading dnspython-1.16.0-py2.py3-none-any.whl (188 kB)
Collecting MarkupSafe>=2.0
Downloading MarkupSafe-2.0.1-cp39-cp39-manylinux2010_x86_64.whl (30 kB)
Installing collected packages: Werkzeug, itsdangerous, click, MarkupSafe, Jinja2, Flask, Six, Flask-Cors, certifi, idna, urllib3, chardet, requests, greenlet, dnspython, eventlet, asyncio, python-dotenv, numpy, scipy
Successfully installed Flask-2.0.0 Flask-Cors-3.0.10 Jinja2-3.0.1 MarkupSafe-2.0.1 Six-1.16.0 Werkzeug-2.0.1 asyncio-3.4.3 certifi-2021.5.30 chardet-3.0.4 click-8.0.1 dnspython-1.16.0 eventlet-0.31.0 greenlet-1.1.0 idna-2.8 itsdangerous-2.0.1 numpy-1.20.3 python-dotenv-0.17.1 requests-2.22.0 scipy-1.6.3 urllib3-1.25.11
-----> Discovering process types
Procfile declares types -> (none)
-----> Compressing...
Done: 100.2M
-----> Launching...
Released v18
https://pid-tuner-condig.herokuapp.com/ deployed to Heroku
URL of Heroku API: https://pid-tuner-condig.herokuapp.com/
URL for GitHub Repository: https://github.com/pmorim/pid-tuner/tree/heroku
Upvotes: 0
Views: 210
Reputation: 172
You are receiving the HTML of the page because the server configuration of your website seems to be something wrong. When you open the link of the website you can see that heroku shows the Application Error indicating this. Have you made a procfile?
Upvotes: 1