[英]Why does my Flask app redirect one route even though I haven't configured it to?
I'm trying to write a POST route in Flask, but everytime I test the route it redirects with a 301 code the same url but as a GET request. 我正在尝试在Flask中编写POST路由,但是每次我测试它使用301代码重定向的路由时,都使用相同的url,但作为GET请求。 I don't want it to, because I need the POST body.
我不想要它,因为我需要POST正文。
I don't see why Flask does this however. 我不明白为什么Flask会这样做。
What's further confusing is that in the same flask application there is another route that does not redirect. 更令人困惑的是,在同一个烧瓶应用程序中,还有另一条不重定向的路由。
I have also tried only setting "POST" as an allowed method, but then the route is still redirected, and the response is a 405 method not allowed, which is logical because I have set that GET is not an allowed method. 我还尝试过仅将“ POST”设置为允许的方法,但是仍将路由重定向,并且响应是不允许的405方法,这是合乎逻辑的,因为我已将GET设置为不允许的方法。 But why does it redirect at all and to itself no less?
但是,为什么它会完全重定向到自身呢?
Access logs: 访问日志:
<redacted ip> - - [14/Feb/2019:11:32:39 +0000] "POST /service HTTP/1.1" 301 194 "-" "python-requests/2.21.0"
<redacted ip> - - [14/Feb/2019:11:32:43 +0000] "GET /service HTTP/1.1" 200 8 "-" "python-requests/2.21.0"
App code, please note that two routes that respectively do not and do redirect: 应用程式程式码,请注意,两个分别不做且确实要重新导向的路线:
def create_app(test_config=None):
# create and configure the app
app = Flask(__name__, instance_relative_config=True)
if test_config is None:
# load the instance config, if it exists, when not testing
app.config.from_pyfile('config.py', silent=True)
else:
# load the test config if passed in
app.config.from_mapping(test_config)
# ensure the instance folder exists
try:
os.makedirs(app.instance_path)
except OSError:
pass
@app.route('/path/does/not/redirect')
def notredirected():
url_for('static', filename='style.css')
return render_template('template.html')
@app.route('/service', strict_slashes=True, methods=["GET", "POST"])
def service():
return "Arrived!"
return app
app = create_app()
My expected result would be that it would be that POST /service
would return 200 and Arrived! 我的预期结果是
POST /service
将返回200并到达! as its output. 作为其输出。
EDIT: If I change the route signature to: @app.route('/service', strict_slashes=True, methods=["POST"])
, it still redirects to a GET request and then returns a 405, because there is no GET route for /service. 编辑:如果我将路由签名更改为:
@app.route('/service', strict_slashes=True, methods=["POST"])
,它仍然会重定向到GET请求,然后返回405,因为没有获取/ service的路线。
<redacted> - - [14/Feb/2019:13:05:27 +0000] "POST /service HTTP/1.1" 301 194 "-" "python-requests/2.21.0"
<redacted> - - [14/Feb/2019:13:05:27 +0000] "GET /service HTTP/1.1" 405 178 "-" "python-requests/2.21.0"
Your service
route has been configured to handle both GET and POST requests , but your service
route doesn't distinguish between incoming GET and POST requests . 您的
service
路由已配置为可以处理GET和POST请求 ,但您的service
路由无法区分传入的GET和POST请求 。 By default if you don't specify the supported methods on your route, flask will default to supporting a GET request . 默认情况下,如果您在路由上未指定支持的方法,则flask将默认支持GET request 。 However, with no check on the incoming request your
service
route doesn't know how to handle the incoming request, since both GET and POST are supported, so it attempts a redirect in order to process the request . 但是,由于不支持对传入请求的检查,因此
service
路由不知道如何处理传入请求,因为同时支持GET和POST,因此它会尝试重定向以处理请求 。 A simple conditional like the following: if flask.request.method == 'POST':
can be used to distinguish between the two types of requests. 一个简单的条件如下:
if flask.request.method == 'POST':
可以用来区分两种类型的请求。 With that being said, maybe you could try out something like the following: 话虽如此,也许您可以尝试以下方法:
@app.route('/service', methods=['GET', 'POST'])
def service():
if request.method == "GET":
msg = "GET Request from service route"
return jsonify({"msg":msg})
else: # Handle POST Request
# get JSON payload from POST request
req_data = request.get_json()
# Handle data as appropriate
msg = "POST Request from service route handled"
return jsonify({"msg": msg})
The problem was that the server redirected all non-https requests to the https variant, which I was unaware of. 问题是服务器将所有非https请求都重定向到了我不知道的https变体。 Since this is not problematic behaviour for us, the solution was specifying in the clients to use https.
由于这对我们来说不是问题,因此解决方案是在客户端中指定使用https。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.