I'm trying to access a mySQL database using the requests library in Python from a REST API I made using Flask, hosted on Heroku. I'm running this script locally. I tried using the postgresql add-on, and it didn't work. I then tried to use a different mySQL db host (freesqldatabase.com) cuz I thought it was a problem with AWS. It still didn't work. I made sure to create the database after deployment by running 'heroku run python, then db.create_all()'. The following error is what I got both times:
2022-06-19T02:31:43.193386 00:00 heroku[router]: at=info method=POST path="/waifulist" host=waifu-list-api.herokuapp.com request_id=89bb900f-1eb8-4cd1-a21e-000c2556421c fwd="99.229.226.167" dyno=web.1 connect=0ms service=1ms status=404 bytes=393 protocol=https
2022-06-19T02:31:43.193281 00:00 app[web.1]: 10.1.53.188 - - [19/Jun/2022:02:31:43 0000] "POST /waifulist HTTP/1.1" 404 232 "-" "python-requests/2.28.0"
Here's how I created the API endpoint:
if __name__ == "__main__":
# Create database
db.create_all()
# Add the Resource class as an API accessible through the given endpoint (i.e. "/waifulist")
api.add_resource(WaifuList, "/waifulist")
# Run app
app.run(debug=True)
The exact script I'm running for the HTTP request is:
import requests
# BASE = "http://127.0.0.1:5000/"
BASE = "https://waifu-list-api.herokuapp.com/"
PASSWORD = "sievers"
# Add first waifu entry
test1 = requests.post(BASE "waifulist", {"id": 1, "name": "Makise Kurisu", "anime": "Steins;Gate", "rank": 1, "password": PASSWORD})
print(test1.json()) # Returns 404
Everything else is on this repo: https://github.com/Chubbyman2/waifu-list-api
Does Heroku simply block HTTP requests then?
CodePudding user response:
As per topsail's reply, apparently the endpoint wasn't being created when I actually deployed my app/API. So I just moved these two lines out, like this:
# Create database
db.create_all()
# Add the Resource class as an API accessible through the given endpoint (i.e. "/waifulist")
api.add_resource(WaifuList, "/waifulist")
if __name__ == "__main__":
# Run app
app.run(debug=True)