corydolphin / flask-cors

Cross Origin Resource Sharing ( CORS ) support for Flask
https://flask-cors.corydolphin.com/
MIT License
873 stars 140 forks source link

Request was aborted before response was completed - Python 3 #295

Closed eduardoroliveira closed 3 years ago

eduardoroliveira commented 3 years ago

My ReactJs application successfully consumes a python 2 / flask with flask-cors web services app.

After migrating the application to python 3, the first endpoint returns successfully but the subsequent calls to a second endpoint are aborted before the response is completed.

The service is responding 200 on the web services side, but the browser aborts the request. Again, the same app works just fine in Python 2.

I have tried all combinations, even the obe below. I am one week trying to solve this but am really frustrated right now...

Any suggestions?

Thanks,

Eduardo

CORS(app,
     supports_credentials=True,
     allow_headers="*",
     expose_headers="*",
     always_send=True,
     resources={r"/api/*": {"origins": "*",
                            "allow_headers": "*",
                            "expose_headers": "*",
                            "supports_credentials": True,
                            "always_send": True
                            }})
corydolphin commented 3 years ago

Hey! Can you please enable debug logs? That should help clarify what’s going on and why. There’s an example of how to enable them in the README.

eduardoroliveira commented 3 years ago

Hi @corydolphin, thank you very much for helping with this.

I collected the logs as you asked. The first call to /api/auth endpoint works just fine. The calls after that do not work. I inverted the failing endpoint as the first call and it started working...

This is the log:

2021-06-04 16:57:48,091 - flask_cors.extension - DEBUG - Request to '/api/auth' matches CORS resource '/*'. Using options: {'origins': ['.*'], 'methods': 'DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT', 'allow_headers': ['.*'], 'expose_headers': None, 'supports_credentials': False, 'max_age': None, 'send_wildcard': False, 'automatic_options': True, 'vary_header': True, 'resources': '/*', 'intercept_exceptions': True, 'always_send': True}
2021-06-04 16:57:48,092 - flask_cors.core - DEBUG - CORS request received with 'Origin' http://localhost:8080
2021-06-04 16:57:48,092 - flask_cors.core - DEBUG - The request's Origin header matches. Sending CORS headers.
2021-06-04 16:57:48,092 - flask_cors.core - DEBUG - Settings CORS headers: MultiDict([('Access-Control-Allow-Origin', 'http://localhost:8080'), ('Vary', 'Origin')])
2021-06-04 16:57:49,488 - flask_cors.extension - DEBUG - Request to '/api/signal' matches CORS resource '/*'. Using options: {'origins': ['.*'], 'methods': 'DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT', 'allow_headers': ['.*'], 'expose_headers': None, 'supports_credentials': False, 'max_age': None, 'send_wildcard': False, 'automatic_options': True, 'vary_header': True, 'resources': '/*', 'intercept_exceptions': True, 'always_send': True}
2021-06-04 16:57:49,488 - flask_cors.core - DEBUG - Settings CORS headers: MultiDict([('Access-Control-Allow-Origin', '*')])
2021-06-04 16:57:50,804 - flask_cors.extension - DEBUG - Request to '/api/signal' matches CORS resource '/*'. Using options: {'origins': ['.*'], 'methods': 'DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT', 'allow_headers': ['.*'], 'expose_headers': None, 'supports_credentials': False, 'max_age': None, 'send_wildcard': False, 'automatic_options': True, 'vary_header': True, 'resources': '/*', 'intercept_exceptions': True, 'always_send': True}
2021-06-04 16:57:50,805 - flask_cors.core - DEBUG - Settings CORS headers: MultiDict([('Access-Control-Allow-Origin', '*')])

This is the browser request /api/signal :

Invoke-WebRequest -Uri "http://localhost:8080/api/signal" -Headers @{
"Pragma"="no-cache"
  "Cache-Control"="no-cache"
  "sec-ch-ua"="`" Not;A Brand`";v=`"99`", `"Google Chrome`";v=`"91`", `"Chromium`";v=`"91`""
  "Accept"="application/json, text/plain, */*"
  "Authorization"="Bearer eyJ0eXA..."
  "sec-ch-ua-mobile"="?0"
  "User-Agent"="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36"
  "Sec-Fetch-Site"="same-origin"
  "Sec-Fetch-Mode"="cors"
  "Sec-Fetch-Dest"="empty"
  "Referer"="http://localhost:8080/login"
  "Accept-Encoding"="gzip, deflate, br"
  "Accept-Language"="en-US,en;q=0.9,es-419;q=0.8,es;q=0.7"
}

I see the Status as (canceled), Size 0 B and Time between 29 and 60 ms ...

eduardoroliveira commented 3 years ago

this is the log for the python 2 version that is working, for reference...

Request to '/api/auth' matches CORS resource '/*'. Using options: {'max_age': None, 'intercept_exceptions': True, 'allow_headers': ['.*'], 'origins': ['.*'], 'vary_header': True, 'methods': 'DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT', 'automatic_options': True, 'send_wildcard': False, 'expose_headers': None, 'supports_credentials': False, 'resources': '/*', 'always_send': True}
CORS request received with 'Origin' http://localhost:8080
The request's Origin header matches. Sending CORS headers.
Settings CORS headers: MultiDict([('Access-Control-Allow-Origin', u'http://localhost:8080'), ('Vary', 'Origin')])
Request to '/api/template' matches CORS resource '/*'. Using options: {'max_age': None, 'intercept_exceptions': True, 'allow_headers': ['.*'], 'origins': ['.*'], 'vary_header': True, 'methods': 'DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT', 'automatic_options': True, 'send_wildcard': False, 'expose_headers': None, 'supports_credentials': False, 'resources': '/*', 'always_send': True}
Request to '/api/drivetype' matches CORS resource '/*'. Using options: {'max_age': None, 'intercept_exceptions': True, 'allow_headers': ['.*'], 'origins': ['.*'], 'vary_header': True, 'methods': 'DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT', 'automatic_options': True, 'send_wildcard': False, 'expose_headers': None, 'supports_credentials': False, 'resources': '/*', 'always_send': True}
Settings CORS headers: MultiDict([('Access-Control-Allow-Origin', '*')])
Settings CORS headers: MultiDict([('Access-Control-Allow-Origin', '*')])
Request to '/api/refresh' matches CORS resource '/*'. Using options: {'max_age': None, 'intercept_exceptions': True, 'allow_headers': ['.*'], 'origins': ['.*'], 'vary_header': True, 'methods': 'DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT', 'automatic_options': True, 'send_wildcard': False, 'expose_headers': None, 'supports_credentials': False, 'resources': '/*', 'always_send': True}
CORS request received with 'Origin' http://localhost:8080
The request's Origin header matches. Sending CORS headers.
Settings CORS headers: MultiDict([('Access-Control-Allow-Origin', u'http://localhost:8080'), ('Vary', 'Origin')])
Request to '/api/signal' matches CORS resource '/*'. Using options: {'max_age': None, 'intercept_exceptions': True, 'allow_headers': ['.*'], 'origins': ['.*'], 'vary_header': True, 'methods': 'DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT', 'automatic_options': True, 'send_wildcard': False, 'expose_headers': None, 'supports_credentials': False, 'resources': '/*', 'always_send': True}
eduardoroliveira commented 3 years ago

Nevermind! Found the problem was nonrelated. The structure of the JWT changed and because that my application keep invalidating the call and retrying, so keep sending a new call aborting the web services calls...

Thanks for your help anyway!

Have a great weekend!

corydolphin commented 3 years ago

Glad to hear it! One request: is there anything you would change in the documentation to help a future version of yourself figure this out more easily?