firebase / firebase-functions-python

Apache License 2.0
136 stars 22 forks source link

firebase use flask route get "ECONNRESET" ,The restful api never response until 60s timeout #159

Open xactester opened 10 months ago

xactester commented 10 months ago
@app.post("/api/test")
def stuff():
    abc = flask.request.get_data()
    # abc= flask.request.get_json();
    print("Jsondata:", abc)
    # name = flask.request.args.get('name')
    # print("name:", name)
    data = {'Message': 'Hello World'}
    return jsonify(data), 200
@https_fn.on_request()
def httpsflaskexample(req: https_fn.Request) -> https_fn.Response:
    with app.request_context(req.environ):
        return app.full_dispatch_request()

A sample like firebase_documents If i remove request.get_data(), It will be ok.

fzrcyc commented 10 months ago

I'm having the same problem when i call flask.request.get_data() in post this is the traceback: Traceback (most recent call last):

File "D:\MyDemo\functions\venv\lib\site-packages\flask\app.py", line 1508, in finalize_request response = self.process_response(response) File "D:\MyDemo\functions\venv\lib\site-packages\flask\app.py", line 2002, in process_response response = self.ensure_sync(func)(response) File "D:\MyDemo\functions\venv\lib\site-packages\functions_framework__init__.py", line 302, in read_request
flask.request.get_data() File "D:\MyDemo\functions\venv\lib\site-packages\werkzeug\wrappers\request.py", line 420, in get_data rv = self.stream.read() File "D:\MyDemo\functions\venv\lib\site-packages\werkzeug\wsgi.py", line 577, in readall data = self.read(1024 * 64) File "D:\MyDemo\functions\venv\lib\site-packages\werkzeug\wsgi.py", line 562, in readinto self.on_disconnect()

stuck in read and then timeout. if post with no body, it's fine.

By the way , i use the firebase emulators

fzrcyc commented 10 months ago

I found a temporary solution.

@https_fn.on_request()
def httpsflaskexample(req: https_fn.Request) -> https_fn.Response:
    with app.request_context(req.environ):
        g.request = req
        return app.full_dispatch_request()

then use g.request in post function

@app.post("/api/test")
def stuff():
    abc = g.request.get_data()
    # abc= flask.request.get_json();
    print("Jsondata:", abc)
    # name = flask.request.args.get('name')
    # print("name:", name)
    data = {'Message': 'Hello World'}
    return jsonify(data), 200
exaby73 commented 8 months ago

Hello @fzrcyc. I am doing a POST request to the URL both through the emulator and on a live deployment using the following command:

# Example for request to local emulator
curl -X POST http://127.0.0.1:5001/ff-test-74aeb/asia-south1/httpsflaskexample/api/test -d '{"key": "value"}' -H "Content-Type: application/json"

I get the following response back:

{"Message":"Hello World"}

This is the code I have in my main.py file:

from firebase_functions import https_fn, options
import flask
from flask import jsonify

app = flask.Flask(__name__)

@app.post("/api/test")
def stuff():
    abc = flask.request.get_data()
    # abc= flask.request.get_json();
    print("Jsondata:", abc)
    # name = flask.request.args.get('name')
    # print("name:", name)
    data = {'Message': 'Hello World'}
    return jsonify(data), 200

options.set_global_options(
    max_instances=10, region=options.SupportedRegion.ASIA_SOUTH1)

@https_fn.on_request()
def httpsflaskexample(req: https_fn.Request) -> https_fn.Response:
    with app.request_context(req.environ):
        return app.full_dispatch_request()

Could you please provide a reproducible example for this issue as I cannot reproduce it using the code sample you provided?

Enlumis commented 6 months ago

Same issue with request.json which calls get_json @fzrcyc work around worked but very hacky lol