1

私は API 開発に足を踏み入れようとしています。ほとんどのメモはこの記事から取っています。

これまでのところ、 、、またはの実行curl requestsに問題はありません。ただし、リクエストはエラーを返しています。GETPOSTDELETEPUT404

私が練習しているAPIコードは次のとおりです。

class UserAPI(Resource):
    def __init__(self):
        self.reqparse = reqparse.RequestParser()
        self.reqparse.add_argument('name', type = str, required = True, help = "No name provided", location = 'json')
        self.reqparse.add_argument('email', type = str, required = True, help = "No email provided", location = 'json')
        self.reqparse.add_argument('password', type = str, required = True, help = "No password provided", location = 'json')
        super(UserAPI, self).__init__()

    def get(self, id):
        if checkUser(id):          #Just checks to see if user with that id exists
            info = getUserInfo(id) #Gets Users info based on id
            return {'id': id, 'name': info[0], 'email':info[1], 'password': info[2], 'role': info[3]}
        abort(404)

    def put(self, id):
        if checkUser(id):
            args = self.reqparse.parse_args()
            deleteUser(id)      #Deletes user with this id
            addUser(User(args['name'], args['email'], args['password'], args['role']))  #Adds user to database 
        abort(404)

    def delete(self, id):
        deleteUser(id)
        return { 'result': True}

class UserListAPI(Resource):
    def __init__(self):
        self.reqparse = reqparse.RequestParser()
        self.reqparse.add_argument('name', type = str, required = True, help = "No name provided", location = 'json')
        self.reqparse.add_argument('email', type = str, required = True, help = "No email provided", location = 'json')
        self.reqparse.add_argument('password', type = str, required = True, help = "No password provided", location = 'json')
        self.reqparse.add_argument('role', type = bool, default = 0, location = 'json')
        super(UserListAPI, self).__init__()

    def get(self):
        return { 'users': map(lambda u: marshal(u, user_fields), getAllUsers()) }

    def post(self):
        print self.reqparse.parse_args()
        args = self.reqparse.parse_args()
        new_user = User(args['name'], args['email'], args['password'], args['role'])
        addUser(new_user)
        return {'user' : marshal(new_user, user_fields)}, 201 

api.add_resource(UserAPI, '/api/user/<int:id>', endpoint = 'user')
api.add_resource(UserListAPI, '/api/users/', endpoint = 'users')

基本的に、1 つのクラスはすべてのユーザーの参照または DB へのユーザーの追加 (UserListAPI) を処理し、他のハンドルは個々のユーザーの取得、ユーザーの更新、またはユーザーの削除 (UserAPI) を処理します。

私が言ったように、すべてが機能しPUTます。

入力するとcurl -H 'Content-Type: application/json' -X PUT -d '{"name": "test2", "email":"test@test.com", "password":"testpass", "role": 0}' http://127.0.0.1:5000/api/user/2

次のエラーが表示されます。

{
    "message": "Not Found. You have requested this URI [/api/user/2] but did you mean /api/user/<int:id> or /api/users/ or /api/drinks/<int:id> ?", 
    "status": 404
}

これは私には意味がありません。<int:id>URL の末尾に付けた整数を受け入れるべきではありませんか?

ご意見ありがとうございます


編集

私の側の愚かなエラーが指摘された後、私の答えを更新します。これで、put メソッドは次のようになります。

def put(self, id):
    if checkUser(id):
        args = self.reqparse.parse_args()
        deleteUser(id)
        user = User(args['name'], args['email'], args['password'], args['role'])
        addUser(user)
        return {'user' : marshal(user, user_fields)}, 201 
    else:
        abort(404)
4

1 に答える 1

1

PUT が成功したときに戻っていないため、常に中止しています (404)。他の HTTP 動詞と同様に:

def put(self, id):
        if checkUser(id):
            args = self.reqparse.parse_args()
            deleteUser(id)      #Deletes user with this id
            addUser(User(args['name'], args['email'], args['password'], args['role']))
            # Missing return when succees
        abort(404) # Always executing

編集:私はあなたの例をテストしました(インポートや実装されていない特定のコードの削除など、機能させるための追加のコードを使用して)。これは私のコードです:

from flask import Flask
from flask.ext.restful import Api, Resource
from flask.ext.restful import reqparse


app = Flask(__name__)
api = Api(app)


class UserAPI(Resource):
    def __init__(self):
        self.reqparse = reqparse.RequestParser()
        self.reqparse.add_argument('name', type = str, required = True, help = "No name provided", location = 'json')
        self.reqparse.add_argument('email', type = str, required = True, help = "No email provided", location = 'json')
        self.reqparse.add_argument('password', type = str, required = True, help = "No password provided", location = 'json')
        super(UserAPI, self).__init__()

    def get(self, id):
        if True: #Just checks to see if user with that id exists
            return {"message": "You have GET me"}
        abort(404)

    def put(self, id):
        if True:
            return {"message": "You have PUT me"}
        abort(404)

    def delete(self, id):
        deleteUser(id)
        return { 'result': True}

class UserListAPI(Resource):
    def __init__(self):
        self.reqparse = reqparse.RequestParser()
        self.reqparse.add_argument('name', type = str, required = True, help = "No name provided", location = 'json')
        self.reqparse.add_argument('email', type = str, required = True, help = "No email provided", location = 'json')
        self.reqparse.add_argument('password', type = str, required = True, help = "No password provided", location = 'json')
        self.reqparse.add_argument('role', type = bool, default = 0, location = 'json')
        super(UserListAPI, self).__init__()

    def get(self):
        return { 'users': map(lambda u: marshal(u, user_fields), getAllUsers()) }

    def post(self):
        print self.reqparse.parse_args()
        args = self.reqparse.parse_args()
        new_user = User(args['name'], args['email'], args['password'], args['role'])
        addUser(new_user)
        return {'user' : marshal(new_user, user_fields)}, 201

api.add_resource(UserAPI, '/api/user/<int:id>', endpoint = 'user')
api.add_resource(UserListAPI, '/api/users/', endpoint = 'users')


if __name__ == "__main__":

    app.run(debug=True)

そして今、私はそれをカールします:

amegian@amegian-Ubuntu:~$ curl -H 'Content-Type: application/json' -X PUT -d '{"name": "test2", "email":"test@test.com", "password":"testpass", "role": 0}' http://127.0.0.1:5000/api/user/2 -v
* About to connect() to 127.0.0.1 port 5000 (#0)
*   Trying 127.0.0.1... connected
> PUT /api/user/2 HTTP/1.1
> User-Agent: curl/7.22.0 (i686-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
> Host: 127.0.0.1:5000
> Accept: */*
> Content-Type: application/json
> Content-Length: 76
> 
* upload completely sent off: 76out of 76 bytes
* HTTP 1.0, assume close after body < HTTP/1.0 200 OK < Content-Type: application/json < Content-Length: 39 < Server: Werkzeug/0.8.3 Python/2.7.3 < Date: Wed, 04 Dec 2013 21:08:40 GMT <  {
    "message": "You have PUT me" }
* Closing connection #0

したがって、削除したメソッドを確認することをお勧めします... checkUser(id) など

于 2013-12-04T20:52:56.670 に答える