flask restful and sqlalchemy error: "uname": "Missing required parameter in the JSON body"
flask restful and sqlalchemy error: "uname": "Missing required parameter in the JSON body"
parser = reqparse.RequestParser()
parser.add_argument('uname', type = unicode, required = True, location= 'json')
parser.add_argument('pword', type = unicode, required = True, location= 'json')
parser.add_argument('fname', type = unicode, required = True, location= 'json')
parser.add_argument('lname', type = unicode, required = True, location= 'json')
parser.add_argument('gender', type = unicode, required = True, location= 'json')
parser.add_argument('email_id', type = unicode, required = True, location= 'json')
session= Session()
class store_user(Resource):
def post(self):
#args=request.get_json(force = True)
args = parser.parse_args()
unq_id = str(uuid.uuid4())
user_data = {'uid' : unq_id, 'username' : args['uname'], 'password' : args['pword'], 'f_name' : args['fname'],
'l_name' : args['lname'], 'sex' : args['gender'], 'email_id' : args['email_id']}
auser = user(uid = user_data['uid'], username = user_data['username'], password = user_data['password'], f_name =
user_data['f_name'], l_name = user_data['l_name'], sex =
user_data['sex'], email_id = user_data['email_id'])
session.add(auser)
session.commit()
#return user_data
#return jsonify(auser)
api.add_resource(store_user, '/user', endpoint='user')
我在向数据库添加新数据时遇到错误。 user 是映射到 user_info table 的 class,user 是用户 class 的对象。我在解析请求数据时遇到问题,但无法解决。以下是使用 curl 命令在终端上的输出:
命令:
curl http://127.0.0.1:5000/user -d "uname= man, pword= uuoiuu, fname= manish, lname= kumar, gender= male, emailid= mkcomp12@gmail.com" -X POST -v
输出:
> * Hostname was NOT found in DNS cache
* Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0)
> POST /user HTTP/1.1
> User-Agent: curl/7.35.0
> Host: 127.0.0.1:5000
> Accept: */*
> Content-Length: 99
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 99 out of 99 bytes
* HTTP 1.0, assume close after body
< HTTP/1.0 400 BAD REQUEST
< Content-Type: application/json
< Content-Length: 90
< Server: Werkzeug/0.9.4 Python/2.7.6
< Date: Thu, 28 Jan 2016 07:31:56 GMT
<
{
"message": {
"uname": "Missing required parameter in the JSON body"
}
}
您必须正确设置输入数据的格式,可以是 JSON(使用 "Content-Type: application/json" header)或 URL 格式。由于您在参数定义中指定了 location=json,因此您必须使用 Content-Type header.
将其格式化为 JSON
这里是你的命令:curl http://127.0.0.1:5000/user -H 'Content-Type: application/json' -d '{"uname":"man", "pword":"uuoiuu", "fname":"manish", "lname":"kumar", "gender":"male", "emailid":"mkcomp12@gmail.com}' -X POST -v
我认为你的 curl 命令有两个问题:
你需要交出-H "Content-Type: application/json"表示你要发送json数据(看这个answer)
你必须交出你的数据作为json数据:
'{"uname":"man","pword":"uuoiuu"}'
parser = reqparse.RequestParser()
parser.add_argument('uname', type = unicode, required = True, location= 'json')
parser.add_argument('pword', type = unicode, required = True, location= 'json')
parser.add_argument('fname', type = unicode, required = True, location= 'json')
parser.add_argument('lname', type = unicode, required = True, location= 'json')
parser.add_argument('gender', type = unicode, required = True, location= 'json')
parser.add_argument('email_id', type = unicode, required = True, location= 'json')
session= Session()
class store_user(Resource):
def post(self):
#args=request.get_json(force = True)
args = parser.parse_args()
unq_id = str(uuid.uuid4())
user_data = {'uid' : unq_id, 'username' : args['uname'], 'password' : args['pword'], 'f_name' : args['fname'],
'l_name' : args['lname'], 'sex' : args['gender'], 'email_id' : args['email_id']}
auser = user(uid = user_data['uid'], username = user_data['username'], password = user_data['password'], f_name =
user_data['f_name'], l_name = user_data['l_name'], sex =
user_data['sex'], email_id = user_data['email_id'])
session.add(auser)
session.commit()
#return user_data
#return jsonify(auser)
api.add_resource(store_user, '/user', endpoint='user')
我在向数据库添加新数据时遇到错误。 user 是映射到 user_info table 的 class,user 是用户 class 的对象。我在解析请求数据时遇到问题,但无法解决。以下是使用 curl 命令在终端上的输出:
命令:
curl http://127.0.0.1:5000/user -d "uname= man, pword= uuoiuu, fname= manish, lname= kumar, gender= male, emailid= mkcomp12@gmail.com" -X POST -v
输出:
> * Hostname was NOT found in DNS cache
* Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0)
> POST /user HTTP/1.1
> User-Agent: curl/7.35.0
> Host: 127.0.0.1:5000
> Accept: */*
> Content-Length: 99
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 99 out of 99 bytes
* HTTP 1.0, assume close after body
< HTTP/1.0 400 BAD REQUEST
< Content-Type: application/json
< Content-Length: 90
< Server: Werkzeug/0.9.4 Python/2.7.6
< Date: Thu, 28 Jan 2016 07:31:56 GMT
<
{
"message": {
"uname": "Missing required parameter in the JSON body"
}
}
您必须正确设置输入数据的格式,可以是 JSON(使用 "Content-Type: application/json" header)或 URL 格式。由于您在参数定义中指定了 location=json,因此您必须使用 Content-Type header.
将其格式化为 JSON这里是你的命令:curl http://127.0.0.1:5000/user -H 'Content-Type: application/json' -d '{"uname":"man", "pword":"uuoiuu", "fname":"manish", "lname":"kumar", "gender":"male", "emailid":"mkcomp12@gmail.com}' -X POST -v
我认为你的 curl 命令有两个问题:
你需要交出-H "Content-Type: application/json"表示你要发送json数据(看这个answer)
你必须交出你的数据作为json数据:
'{"uname":"man","pword":"uuoiuu"}'