PanczerTank
PanczerTank

Reputation: 1130

Issue with accessing CRUD endpoint in Flask

I created a CRUD endpoint wit Flask but when I try to GET data, I receive a 404 error. I tried to access this endpoint with 'http://127.0.0.1:5002/albums/beck//' and 'http://127.0.0.1:5002/albums/beck' but still get a 404. Since I supplied 'beck' as the artist name I thought the get method would run fine. I think I added the resource incorrectly.

class Artistdetails(Resource):
    def get(self, artist_name):
        conn = db_connect.connect()
        # Protect against SQL injection
        restricted_char = "!=<>*0&|/\\"
        for char in restricted_char:
            artist_name = artist_name.replace(char, "")
        query_db = conn.execute("SELECT DISTINCT album FROM album WHERE artist='{0}'".format(artist_name.title()))
        result = jsonify({'artistAlbumList': [i[0] for i in query_db.cursor.fetchall()]})
        return result

    def put(self, artist_name, album_name, album_name_new):
        conn = db_connect.connect()
        # Protect against SQL injection
        restricted_char = "!=<>*0&|/\\"
        for char in restricted_char:
            artist_name = artist_name.replace(char, "")
        query_db = conn.execute("UPDATE album SET album='{0}' WHERE artist='{1}' AND"
                                " album='{2}'".format(artist_name.title(), album_name.title(), album_name_new.title()))
        result = jsonify({'putAlbumId': [i[0] for i in query_db.cursor.fetchall()]})
        return result, 201

    def post(self, artist_name, album_name):
        conn = db_connect.connect()
        # Protect against SQL injection
        restricted_char = "!=<>*0&|/\\"
        for char in restricted_char:
            artist_name = artist_name.replace(char, "")
        query_db = conn.execute("INSERT INTO album (album, artist) VALUES"
                                " ({0},{1})".format(artist_name.title(), album_name.title()))
        result = jsonify({'postAlbumId': [i[0] for i in query_db.cursor.fetchall()]})
        return result, 201

    def delete(self, artist_name, album_name):
        conn = db_connect.connect()
        # Protect against SQL injection
        restricted_char = "!=<>*0&|/\\"
        for char in restricted_char:
            artist_id = artist_name.replace(char, "")
            album_id = album_name.replace(char, "")
        query_db = conn.execute("DELETE FROM album WHERE"
                                " artist_id='{0}' AND album_id='{1}'".format(artist_name, album_name)
                                )
        result = jsonify({'deleteAlbumId': [i[0] for i in query_db.cursor.fetchall()]})
        return result, 204

Create API routes

api.add_resource(Api, '/')
api.add_resource(Albums, '/albums')
api.add_resource(Artistdetails, '/albums/<string:artist_name>/<string:album_name>/<string:album_name_new>')
api.add_resource(Genreyear, '/albums/yr')
api.add_resource(Genrenum, '/albums/genre')
api.add_resource(Artists, '/artists')

Upvotes: 0

Views: 97

Answers (1)

wkl
wkl

Reputation: 79893

This line:

api.add_resource(Artistdetails,
    '/albums/<string:artist_name>/<string:album_name>/<string:album_name_new>')

It adds a path to the Flask router that makes it expect /albums/<artist_name>/<album_name>/<album_name_new>, whereas you're trying to request /albums/<artist_name>, which doesn't match anything.

A quick fix for you would be:

api.add_resource(Artistdetails, '/albums/<string:artist_name>')

However, you might instead want to support query string parameters for your search interface so that requests look more like this:

/albums?artist=<string>&album_name=<string>

To do that, the documentation for Flask-RESTful reqparse would be useful.

Upvotes: 1

Related Questions