Search code examples
pythonpython-3.xflaskflask-ask

Flask-Ask: How to fix 'flask.debughelpers.FormDataRoutingRedirect' error?


I'm developing Alexa Skill with Flask-Ask. When I test my skill, error are thrown.

I using nginx proxy for Flask-Ask.

(Previously, it worked fine on another server.) Now, the another server has same problem. So I think there is another problem, not the code. I also saw Flask Message Flashing error - flask.debughelpers.FormDataRoutingRedirect, but it didn't solve.

Code:

from flask import Flask
from flask_ask import Ask, statement

app = Flask(__name__)
ask = Ask(app, '/')

@ask.launch
def launch():
    speech_text = "Hello!"
    return statement(speech_text).simple_card('Hello', speech_text)

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

Console output:

127.0.0.1 - - [15/May/2019 10:41:03] "POST  HTTP/1.0" 500 -
Traceback (most recent call last):
  File "/home/user/.local/lib/python3.7/site-packages/flask/app.py", line 1997, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/user/.local/lib/python3.7/site-packages/flask/app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "/home/user/.local/lib/python3.7/site-packages/flask/app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/user/.local/lib/python3.7/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/home/user/.local/lib/python3.7/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/user/.local/lib/python3.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/user/.local/lib/python3.7/site-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/user/.local/lib/python3.7/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/home/user/.local/lib/python3.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/user/.local/lib/python3.7/site-packages/flask/app.py", line 1590, in dispatch_request
    self.raise_routing_exception(req)
  File "/home/user/.local/lib/python3.7/site-packages/flask/app.py", line 1576, in raise_routing_exception
    raise FormDataRoutingRedirect(request)
flask.debughelpers.FormDataRoutingRedirect: b'A request was sent to this URL (http://127.0.0.1:5000/) but a redirect was issued automatically by the routing system to "http://127.0.0.1:5000/".  Make sure to directly send your POST-request to this URL since we can't make browsers or HTTP clients redirect with form data reliably or without user interaction.  Note: this exception is only raised in debug mode'
127.0.0.1 - - [15/May/2019 10:41:05] "POST  HTTP/1.0" 500 -
Traceback (most recent call last):
  File "/home/user/.local/lib/python3.7/site-packages/flask/app.py", line 1997, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/user/.local/lib/python3.7/site-packages/flask/app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "/home/user/.local/lib/python3.7/site-packages/flask/app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/user/.local/lib/python3.7/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/home/user/.local/lib/python3.7/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/user/.local/lib/python3.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/user/.local/lib/python3.7/site-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/user/.local/lib/python3.7/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/home/user/.local/lib/python3.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/user/.local/lib/python3.7/site-packages/flask/app.py", line 1590, in dispatch_request
    self.raise_routing_exception(req)
  File "/home/user/.local/lib/python3.7/site-packages/flask/app.py", line 1576, in raise_routing_exception
    raise FormDataRoutingRedirect(request)
flask.debughelpers.FormDataRoutingRedirect: b'A request was sent to this URL (http://127.0.0.1:5000/) but a redirect was issued automatically by the routing system to "http://127.0.0.1:5000/".  Make sure to directly send your POST-request to this URL since we can't make browsers or HTTP clients redirect with form data reliably or without user interaction.  Note: this exception is only raised in debug mode'

nginx config:

server {
    listen       80;
    server_name  mydomain.com;
    return 301 https://$host$request_uri;
}

server {
    listen       443 ssl;
    server_name  mydomain.com;

    ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem;

    location / {
        deny  all;
    }

    location /alexa {
        proxy_pass http://127.0.0.1:5000/;
    }
}

My environment:

  • Ubuntu 19.04
  • Python 3.7.3
  • nginx/1.15.12

Solution

  • To solve this problem...

    1. Edit nginx config

    Remove the trailing / from proxy_pass.

    Before
    proxy_pass http://127.0.0.1:5000/;
    
    After
    proxy_pass http://127.0.0.1:5000;
    

    2. Edit python code

    It need to match with nginx config location /alexa {.

    Before
    ask = Ask(app, '/')
    
    After
    ask = Ask(app, '/alexa/')
    

    3. (Option?) Reinstall pyOpenSSL

    If you have error like this...

    AttributeError: module 'lib' has no attribute 'X509V3_EXT_get'
    

    Reinstall pyOpenSSL. I installed pyOpenSSL 19.0.0.

    terminal
    pip3 uninstall pyOpenSSL
    pip3 install pyOpenSSL