Python PUT curl请求返回错误的URI(RESTful)
我正在努力让我的脚湿在API开发。我的大部分笔记都是从学校里记的 到目前为止,我对Python PUT curl请求返回错误的URI(RESTful),python,api,rest,flask-extensions,Python,Api,Rest,Flask Extensions,我正在努力让我的脚湿在API开发。我的大部分笔记都是从学校里记的 到目前为止,我对GET、POST或DELETE执行curl请求没有任何问题放置请求返回一个404错误 下面是我正在练习的API代码: class UserAPI(Resource): def __init__(self): self.reqparse = reqparse.RequestParser() self.reqparse.add_argument('name', type = st
GET
、POST
或DELETE
执行curl请求没有任何问题<代码>放置
请求返回一个404
错误
下面是我正在练习的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')
这对我来说毫无意义。
不应该接受我放在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)
当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)
从烧瓶导入烧瓶
来自flask.ext.restful导入Api,资源
从flask.ext.restful导入
app=烧瓶(名称)
api=api(应用程序)
类UserAPI(资源):
定义初始化(自):
self.reqparse=reqparse.RequestParser()
self.reqparse.add_参数('name',type=str,required=True,help=“未提供名称”,location='json')
self.reqparse.add_参数('email',type=str,required=True,help=“未提供电子邮件”,location='json')
self.reqparse.add_参数('password',type=str,required=True,help=“未提供密码”,location='json')
super(UserAPI,self)。\uuuu init\uuuuu()
def get(自我,id):
如果为True:#只需检查具有该id的用户是否存在
返回{“消息”:“你已经找到我了”}
中止(404)
def put(自我,id):
如果为真:
return{“message”:“youhaveput me”}
中止(404)
def delete(自我,id):
删除用户(id)
返回{'result':True}
类UserListAPI(资源):
定义初始化(自):
self.reqparse=reqparse.RequestParser()
self.reqparse.add_参数('name',type=str,required=True,help=“未提供名称”,location='json')
self.reqparse.add_参数('email',type=str,required=True,help=“未提供电子邮件”,location='json')
self.reqparse.add_参数('password',type=str,required=True,help=“未提供密码”,location='json')
self.reqparse.add_参数('role',type=bool,default=0,location=json')
super(UserListAPI,self)。\uuuu init\uuuu()
def get(自我):
返回{'users':映射(lambda u:marshal(u,user_字段),getAllUsers())}
def post(自我):
打印self.reqparse.parse_args()
args=self.reqparse.parse_args()
新用户=用户(args['name']、args['email']、args['password']、args['role'])
addUser(新用户)
返回{'user':封送(新用户,用户字段)},201
add_资源(UserAPI,'/api/user/',endpoint='user')
add_资源(UserListAPI,'/api/users/',endpoint='users')
如果名称=“\uuuuu main\uuuuuuuu”:
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
amegian@amegian-Ubuntu:~$curl-H'内容类型:application/json'-X PUT-d'{“name”:“test2”,“email”:test@test.com“,”密码“:”测试通行证“,”角色“:0}”http://127.0.0.1:5000/api/user/2 -五
*即将连接()到127.0.0.1端口5000(#0)
*正在尝试127.0.0.1。。。有联系的
>PUT/api/user/2http/1.1
>用户代理: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
>主持人:127.0.0.1:5000
>接受:*/*
>内容类型:application/json
>内容长度:76
>
*上传已完全发送:76字节中的76字节
*HTTP 1.0,假设正文后关闭
所以我鼓励你检查我删除的那些方法。。。就像checkUser(id)当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)
从烧瓶导入烧瓶
来自flask.ext.restful导入Api,资源
从flask.ext.restful导入
app=烧瓶(名称)
api=api(应用程序)
类UserAPI(资源):
定义初始化(自):
self.reqparse=reqparse.RequestParser()
self.reqparse.add_参数('name',type=str,required=True,help=“未提供名称”,location='json')
self.reqparse.add_参数('email',type=str,required=True,help=“未提供电子邮件”,location='json')
self.reqparse.add_参数('password',type=str,required=True,help=“未提供密码”,location='json')
super(UserAPI,self)。\uuuu init\uuuuu()
def get(自我,id):
如果为True:#只需检查具有该id的用户是否存在
返回{“消息”:“你已经找到我了”}
中止(404)
def put(自我,id):
如果为真:
return{“message”:“youhaveput me”}
中止(404)
def delete(自我,id):
删除用户(id)
返回{'result':True}
类UserListAPI(资源):
定义初始化(自):
self.reqparse=reqparse.RequestParser()
self.reqparse.add_参数('name',type=str,required=True,help=“未提供名称”,location='json')
self.reqparse.add_参数('email',type=str,required=True,help=“未提供电子邮件”,location='json')
self.reqparse.add_参数('password',type=str,required=True,help=“未提供密码”,location='json')
self.reqparse.add_参数('role',type=bool,default=0,location=json')
super(UserListAPI,self)。\uuuu init\uuuu()
def get(自我):
返回{'users':映射(lambda u:marshal(u,user_字段),getAllUsers())}