Flask,具有默认值和方法的蓝图路由,FormDataRoutingRedirect



我有一条路线,如下所示:

blueprint = Blueprint('graphql', 'graphql_v0')
@blueprint.route('/', methods=['POST'])
def graphql_query():
...

这工作正常。我想添加另一个带有变量的路径,这意味着我需要为第一个函数提供默认值:

@blueprint.route('/', methods=['POST'], defaults={'component_name': 'app'})
@blueprint.route('/<component_name>', methods=['POST'])
def graphql_query(component_name):
...

这正在崩溃并显示以下错误:

FormDataRoutingRedirect:已向此 URL (http://localhost:3000/graphql?hash=13b94387f3dcac8ad4252eb7ea4aa12b) 发送请求,但路由系统自动向"http://localhost:3000/v0/graphql/?hash=13b94387f3dcac8ad4252eb7ea4aa12b"发出重定向。 确保直接将您的 POST 请求发送到此 URL,因为我们无法可靠地使用表单数据或无需用户交互即可使浏览器或 HTTP 客户端重定向。

我无法根据我现有的代码理解此错误。

好吧。 这种方法有什么问题?(只需传递函数参数的默认值)

@blueprint.route("/", methods=['POST'])
@blueprint.route("/<component_name>", methods=['POST'])
def graphql_query(component_name="app"):
# ....
return str(component_name)

此代码创建一个双路由函数,其中/(根)和/<component_name>(根和组件名称)作为路由,并返回在 POST 请求中传递的componentname

测试它是否有效(我将使用 curl,与在浏览器中进行 POST 相同):

❱  curl -X POST http://127.0.0.1:5000/app
app%
❱  curl -X POST http://127.0.0.1:5000/
app%
❱  curl -X POST http://127.0.0.1:5000/just_a_component
just_a_component%
❱  curl -X POST http://127.0.0.1:5000/its_working
its_working%
# and doing a GET will raise an error.
❱  curl http://127.0.0.1:5000/its_working
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>405 Method Not Allowed</title>
<h1>Method Not Allowed</h1>
<p>The method is not allowed for the requested URL.</p>

如您所见,对我来说它有效。

去拿这个片段,告诉我它是否有效。和平。

相关内容

  • 没有找到相关文章

最新更新