在烧瓶路线中允许所有的方法类型 [英] Allow ALL method types in flask route
本文介绍了在烧瓶路线中允许所有的方法类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我怎样才能允许一个路径接受所有类型的方法?
我不只是想像 HEAD这样的标准方法,
GET
, POST
,选项
, DELETE
& PUT
。
我也想接受以下方法: FOOBAR
,
WHYISTHISMETHODNAMESOLONG
& 所有其他可能的方法名称。
解决方案
您可以直接更改url_map, 规则
没有方法:
from flask import烧瓶,请求
import unittest $ b $ from werkzeug.routing import Rule
$ b app = Flask(__ name__)
app.url_map.add(Rule('/',endpoint ='index'))
@ app.endpoint 'index')
def index():
return request.method
class TestMethod(unittest.TestCase):
def setUp(self):
self.client = app.test_client()
$ b $ def test_custom_method(self):
resp = self.client.open('/',method = 'BACON')
self.assertEqual('BACON',resp.data)
if __name__ =='__main__':
unittest.main()
这个规则适用的一系列http方法。如果没有指定,所有的方法都是允许的。
How can I allow a route to accept all types of methods?
I don't just want to route the standard methods like HEAD
, GET
, POST
, OPTIONS
, DELETE
& PUT
.
I would like it to also accept the following methods: FOOBAR
, WHYISTHISMETHODNAMESOLONG
& every other possible method names.
解决方案
You can change the url_map directly for this, by adding a Rule
with no methods:
from flask import Flask, request
import unittest
from werkzeug.routing import Rule
app = Flask(__name__)
app.url_map.add(Rule('/', endpoint='index'))
@app.endpoint('index')
def index():
return request.method
class TestMethod(unittest.TestCase):
def setUp(self):
self.client = app.test_client()
def test_custom_method(self):
resp = self.client.open('/', method='BACON')
self.assertEqual('BACON', resp.data)
if __name__ == '__main__':
unittest.main()
methods
A sequence of http methods this rule applies to. If not specified, all methods are allowed.
这篇关于在烧瓶路线中允许所有的方法类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文