I'm Creating an endpoint that retrieves the number of each objects by type. when i execute the code with:
curl -X GET http://0.0.0.0:5000/api/v1/stats
it says:
curl: (7) Failed to connect to 0.0.0.0 port 5000: Connection refused
i've been trying to see if I have a config file, checked my listed ports, Google on how to change/activate the port, & still haven't found something.
This is my full code:
''' module that makes this an app '''
from flask import Flask, abort, jsonify
from models import storage
from api.v1.views import app_views
from os import getenv
app = Flask(__name__)
app.register_blueprint(app_views)
@app.teardown_appcontext
def close(self):
''' method that close the yes '''
storage.close()
@app.errorhandler(404)
def invalid_route(e):
return jsonify({"error": "Not found"}), 404
if __name__ == '__main__':
app.run(host=getenv("HBNB_API_HOST", "0.0.0.0"),
port=int(getenv("HBNB_API_PORT", "5000")), threaded=True)```
CodePudding user response:
Your code is completely right, you are not sending the request to the right host.
Your flask server is binded to 0.0.0.0
, which means that your server will be bounded to the default address (either localhost
or your actual ip address)
This should be fine:
curl -X GET http://localhost:5000/api/v1/stats
CodePudding user response:
I used an executable command like this:
HBNB_MYSQL_USER=hbnb_dev HBNB_MYSQL_PWD=hbnb_dev_pwd HBNB_MYSQL_HOST=localhost HBNB_MYSQL_DB=hbnb_dev_db HBNB_TYPE_STORAGE=db HBNB_API_HOST=0.0.0.0 HBNB_API_PORT=5000 sudo python3 -m api.v1.app
but I forgot to add sudo
before python3
. Then you should open a new terminal and execute the curl command. THANKS