I have a flask login route that is supposed to return an access token
My Route looks like,
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
Currently, in a separate terminal when I run a requests.post()
it just returns, {'email': None, 'password': None}
and not the access_token. I can see the access_token
in my logs of my flask app so it creates that variable.
My request looks like,
creds = requests.post(
login_url,
data={'email': 'wally@starlabs.com', 'password': 'west'}
).json()
In [2]: creds
Out[2]: {'email': None, 'password': None}
What am I missing?
Update:
If I change my LoginApi
to,
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
I can run,
creds = requests.post(
login_url,
json={'email': 'wally@starlabs.com', 'password': 'west'}
).json()
In [4]: creds
Out[4]: {'access_token': 'a-jwt-string'}
But if I run requests.post
with data
instead,
creds = requests.post(
login_url,
data={'email': 'wally@starlabs.com', 'password': 'west'}
).json()
I get Out[6]: {'message': 'Failed to decode JSON object: Expecting value: line 1 column 1 (char 0)'}
I get the same error if I run, curl http://localhost:5000/api/login -X POST -d '{"email":"wally@starlabs.com","password":"west"}'