Flask-RESTplus Upload image with token authentication

184 views Asked by At

I make simple Flask REST-API app. There are just two fields:

upload_parser.add_argument('photo', location='files', type=FileStorage)

upload_parser.add_argument('group', location='form', choices=[1,2,3], help='Bad value for group')

Everything works fine, image uploading, saving... json response displayed fine... But, when I added authentication, there is a problem with 401 response ('Unauthorized').

What is strange, if I post just 'group' without 'photo', 401 response is displayed correctly.

I used this code:

def token_required(f):
@wraps(f)
def decorated(*args, **kwargs):

    token = None

    if 'X-API-KEY' in request.headers:
        token = request.headers['X-API-KEY']

    if not token:
        response_object = {
            'message': 'Token is missing!'
        }
        return make_response(jsonify(response_object), 401)


    if token != 'mytoken':
        response_object = {
            'message': 'Could not verify your login!!'
        }
        return make_response(jsonify(response_object), 401)

    print('TOKEN: {}'.format(token))
    return f(*args, **kwargs)

And then I just call it in the post:

@token_required
def post(self):

   return {"results": "OK"}

So, if I don't send 'photo' there is a response :

{
  "message": "Token is missing!"
}

But with photo in POST, the response is missing.

Note: If I send X-API-KEY in header, everything works fine.

Any idea here?

0

There are 0 answers