Error in route execution on Flask: "missing 1 required positional argument" - Google Cloud Function

Viewed 138

Developing a simple function to be deployed in the Google Cloud Function, a detail has come up. When using a parameter request as a function's argument, the code is not executed locally but is executed in cloud env. When not using request as an argument the code works fine locally but crashes in the cloud env.

  • def hello_http(request): not works locally / works fine in cloud
  • def hello_http(): works fine locally / not works in cloud

Current state running locally:

import firebase_admin
from firebase_admin import auth
import flask
from flask import Flask
from flask import request
from flask_cors import CORS

app = Flask(__name__)
CORS(app)
firebase_admin.initialize_app()

@app.route('/hello_http', methods=['GET'])
def hello_http(request):
    if 'token' in request.args:
        uid = validate(request)
        if uid == False:
            return 401
        else:            
            response = flask.jsonify({'UID': uid})
            print('Headers: ' + str(response.headers))
            print('UID: ' + str(uid))
            return response
    else:
        return 'bad request!', 400

def validate(request):
    try:
        decoded_token = auth.verify_id_token(request.args['token'], check_revoked=True)
        uid = decoded_token['uid']
        return uid
    except Exception as e:
        return False

if __name__ == '__main__':
    app.run()

Invocation:

curl -XGET "http://127.0.0.1:5000/hello_http?token=2"

Output:

[2022-07-04 21:13:00,234] ERROR in app: Exception on /hello_http [GET]
Traceback (most recent call last):
  File "/Users/agstcadini/opt/anaconda3/lib/python3.9/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/agstcadini/opt/anaconda3/lib/python3.9/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/agstcadini/opt/anaconda3/lib/python3.9/site-packages/flask_cors/extension.py", line 165, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/Users/agstcadini/opt/anaconda3/lib/python3.9/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/agstcadini/opt/anaconda3/lib/python3.9/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/Users/agstcadini/opt/anaconda3/lib/python3.9/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/agstcadini/opt/anaconda3/lib/python3.9/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
TypeError: hello_http() missing 1 required positional argument: 'request'
0 Answers
Related