我有一个烧瓶登录路由,应该返回一个访问令牌
“我的路线”看起来像
auth_api = Api(auth_bp)
parser = auth_api.parser()
auth = auth_api.model('Auth', {
'email': fields.String(required=True, description='Email'),
'password': fields.String(required=True, description='Password'),
})
parser.add_argument('email', type=str, required=True, help='Email', location='form')
parser.add_argument('password', type=str, required=True, help='Password', location='form')
class LoginApi(Resource):
@auth_api.marshal_with(auth, code=201)
def post(self):
req = parser.parse_args()
print(req)
email = req.get('email', None)
print(f"Email: {email}")
password = req.get('password', None)
print(f"Password: {password}")
user = guard.authenticate(email, password)
print(user)
access_token = {'access_token': guard.encode_jwt_token(user)}
print(access_token)
return access_token, 201
当前,在另一个终端中,当我运行requests.post()
时,它仅返回{'email': None, 'password': None}
而不是access_token。我可以在烧瓶应用程序的日志中看到access_token
,以便它创建该变量。
我的请求看起来像
creds = requests.post(
login_url,
data={'email': 'wally@starlabs.com', 'password': 'west'}
).json()
In [2]: creds
Out[2]: {'email': None, 'password': None}
我想念什么?
更新:
如果我将LoginApi
更改为
class LoginApi(Resource):
def post(self):
req = request.get_json(force=True)
email = req.get('email', None)
password = req.get('password', None)
user = guard.authenticate(email, password)
access_token = {'access_token': guard.encode_jwt_token(user)}
return access_token
我可以跑步
creds = requests.post(
login_url,
json={'email': 'wally@starlabs.com', 'password': 'west'}
).json()
In [4]: creds
Out[4]: {'access_token': 'a-jwt-string'}
但是如果我改用requests.post
运行data
,
creds = requests.post(
login_url,
data={'email': 'wally@starlabs.com', 'password': 'west'}
).json()
我得到Out[6]: {'message': 'Failed to decode JSON object: Expecting value: line 1 column 1 (char 0)'}
如果我跑步,我会遇到相同的错误,curl http://localhost:5000/api/login -X POST -d '{"email":"wally@starlabs.com","password":"west"}'