I'm trying to make an api call using Postman, from a simple app written in Python shown here:
from flask import Flask
from flask_restful import Resource, Api
app = Flask(__name__)
api = Api(app)
if __name__ == '__main__':
app.run()
class Users(Resource):
pass
api.add_resource(Users, '/users')
When running the code, I get the response:
- Serving Flask app 'main' (lazy loading) * Environment: production
- WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
- Debug mode: off
- Running on http://127.0.0.1:5000/ (Press CTRL C to quit)
When I make a get request to the address below, i receive a 404 error:
I tried rebooting my pc (saw someone fixed it that way), and using Insomnia instead of Postman. I tried adding changing the users class section, that did not fix it either:
from flask import Flask
from flask_restful import Resource, Api
import pandas as pd
app = Flask(__name__)
api = Api(app)
if __name__ == '__main__':
app.run()
class Users(Resource):
def get(self):
data = pd.read_csv('users.csv')
data = data.to_dict()
return {'data': data}, 200
api.add_resource(Users, '/users')
CodePudding user response:
Found it, this works:
from flask import Flask
from flask_restful import Resource, Api
import pandas as pd
app = Flask(__name__)
api = Api(app)
class Users(Resource):
def get(self):
data = pd.read_csv('users.csv') # read CSV
data = data.to_dict() # convert dataframe to dictionary
return {'data': data}, 200 # return data and 200 OK code
api.add_resource(Users, '/users') # '/users' is our entry point for Users
if __name__ == '__main__':
app.run() # run our Flask app
CodePudding user response:
You are missing the route prefix for your view. Try this:
@app.route('/')
class Users(Resource):
def get(self):
data = pd.read_csv('users.csv')
data = data.to_dict()
return {'data': data}, 200