noirbizarre / flask-restplus

Fully featured framework for fast, easy and documented API development with Flask
http://flask-restplus.readthedocs.org
Other
2.73k stars 506 forks source link

How do I get back the value of a response in a post request? #601

Open spitfiredd opened 5 years ago

spitfiredd commented 5 years ago

I have a login route that returns a jwt,

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')
parser.add_argument('password', type=str, required=True, help='Password')

class LoginApi(Resource):

    @auth_api.marshal_with(auth, code=201)
    def post(self):
        # req = request.get_json(force=True)
        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 (jsonify(access_token), 201)

auth_api.add_resource(LoginApi, '/login')

I can't seem to return the access_token?

For example in s separate terminal i can run,

import requests
login_url = 'http://localhost:5000/api/login'
creds = requests.post(
    login_url,
    json={'email': 'wally@starlabs.com', 'password': 'west'}
).json()

or

creds = requests.post(
    login_url,
    data={'email': 'wally@starlabs.com', 'password': 'west'}
).json()

I can watch my logs, and it shows,

{'email': 'wally@starlabs.com', 'password': 'west'}
Email: wally@starlabs.com
Password: west
<User 1>
{'access_token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1NTE4MDk2MTYsImV4cCI6MTU1MTg5NjAxNiwicmZfZXhwIjoxNTUxOTgyNDE2LCJqdGkiOiJiMWUzMzhhZi05YTFlLTQzMzMtYjZlMS1jMGU3YjNkZDc2YTIiLCJpZCI6MSwicmxzIjoiIn0.hLMjQbeHX8UkNPu-xO7hE7z5z6Vld-Hq1Du3curCR6E'}
127.0.0.1 - - [05/Mar/2019 13:13:36] "POST /api/login HTTP/1.1" 201 -

But it doesn't return the access_token, e.g.

>>> creds
{'email': None, 'password': None}

Why is it not returning my access_token?

Colin-b commented 5 years ago

You are marshalling with a model not containing what you send, so that's expected behaviour.

On Tue, Mar 5, 2019, 19:20 Daniel Donovan notifications@github.com wrote:

I have a login route that returns a jwt,

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') parser.add_argument('password', type=str, required=True, help='Password')

class LoginApi(Resource): @auth_api.doc(parser=parser) @auth_api.marshal_with(auth, code=201) def post(self):

req = request.get_json(force=True)

    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 (jsonify(access_token), 201)

auth_api.add_resource(LoginApi, '/login')

I can't seem to return the access_token?

For example in s separate terminal i can run,

import requests login_url = 'http://localhost:5000/api/login' creds = requests.post( login_url, json={'email': 'wally@starlabs.com', 'password': 'west'} ).json()

or

creds = requests.post( login_url, data={'email': 'wally@starlabs.com', 'password': 'west'} ).json()

I can watch my logs, and it shows,

{'email': 'wally@starlabs.com', 'password': 'west'} Email: wally@starlabs.com Password: west <User 1> {'access_token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1NTE4MDk2MTYsImV4cCI6MTU1MTg5NjAxNiwicmZfZXhwIjoxNTUxOTgyNDE2LCJqdGkiOiJiMWUzMzhhZi05YTFlLTQzMzMtYjZlMS1jMGU3YjNkZDc2YTIiLCJpZCI6MSwicmxzIjoiIn0.hLMjQbeHX8UkNPu-xO7hE7z5z6Vld-Hq1Du3curCR6E'} 127.0.0.1 - - [05/Mar/2019 13:13:36] "POST /api/login HTTP/1.1" 201 -

But it doesn't return the access_token, e.g.

creds {'email': None, 'password': None}

Why is it not returning my access_token?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/noirbizarre/flask-restplus/issues/601, or mute the thread https://github.com/notifications/unsubscribe-auth/AHPX_bX_lrJZ_WEpfSnvzbsJVUIj8pqlks5vTrWHgaJpZM4bfN5P .

Colin-b commented 5 years ago

Use expect instead of marshal_with if you want to document what is expected (you can use the model or the parser if I recall properly)

On Tue, Mar 5, 2019, 20:07 Colin B colin.bounouar@gmail.com wrote:

You are marshalling with a model not containing what you send, so that's expected behaviour.

On Tue, Mar 5, 2019, 19:20 Daniel Donovan notifications@github.com wrote:

I have a login route that returns a jwt,

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') parser.add_argument('password', type=str, required=True, help='Password')

class LoginApi(Resource): @auth_api.doc(parser=parser) @auth_api.marshal_with(auth, code=201) def post(self):

req = request.get_json(force=True)

    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 (jsonify(access_token), 201)

auth_api.add_resource(LoginApi, '/login')

I can't seem to return the access_token?

For example in s separate terminal i can run,

import requests login_url = 'http://localhost:5000/api/login' creds = requests.post( login_url, json={'email': 'wally@starlabs.com', 'password': 'west'} ).json()

or

creds = requests.post( login_url, data={'email': 'wally@starlabs.com', 'password': 'west'} ).json()

I can watch my logs, and it shows,

{'email': 'wally@starlabs.com', 'password': 'west'} Email: wally@starlabs.com Password: west <User 1> {'access_token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1NTE4MDk2MTYsImV4cCI6MTU1MTg5NjAxNiwicmZfZXhwIjoxNTUxOTgyNDE2LCJqdGkiOiJiMWUzMzhhZi05YTFlLTQzMzMtYjZlMS1jMGU3YjNkZDc2YTIiLCJpZCI6MSwicmxzIjoiIn0.hLMjQbeHX8UkNPu-xO7hE7z5z6Vld-Hq1Du3curCR6E'} 127.0.0.1 - - [05/Mar/2019 13:13:36] "POST /api/login HTTP/1.1" 201 -

But it doesn't return the access_token, e.g.

creds {'email': None, 'password': None}

Why is it not returning my access_token?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/noirbizarre/flask-restplus/issues/601, or mute the thread https://github.com/notifications/unsubscribe-auth/AHPX_bX_lrJZ_WEpfSnvzbsJVUIj8pqlks5vTrWHgaJpZM4bfN5P .

spitfiredd commented 5 years ago

I am not looking to document it, it removed that line, my apologies.

ziirish commented 5 years ago

Hi, Is your problem fixed then?