Reputation: 555
I recently completed a flask API tutorial from YouTube and I am now in the process of replicating what I learned in my own back end. Unfortunately after doing my own version of a what is only a little bit more complicated than a hello world in flask, I am getting a 404 error on my endpoints.
Here is my code so far: from flask import Flask, request from flask_restful import Api, Resource, reqparse, fields, marshal_with
app = Flask(__name__)
api = Api(app)
class Player(Resource):
def get(self):
return {"data" :"Hello"}
def post(self):
return 'Posted',200
if __name__ =="__main__":
app.run(debug=True)
I have tried making a request to both endpoints ( get & post to http://127.0.0.1:5000/player) and have done so from using a python script like the one below, a get request from my browser, and get & post requests from postman, and none have made my endpoints work. I have tried reinstalling flask and flask_restful with pip as well. I've also tried changing what I return, by either changing the object in my get request to a string, adding status codes, and removing status codes.
import requests
BASE = "http://127.0.0.1:5000/"
response = requests.get(BASE + 'player')
print(response.json())
Would anyone know what I am doing wrong?
Upvotes: 0
Views: 688
Reputation: 11223
You forgot api.add_resource(Player, '/player')
see example in docs
Upvotes: 2