是否有可能在Flask中发出POST请求? [英] Is it possible to make POST request in Flask?
本文介绍了是否有可能在Flask中发出POST请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
让我们想象一下:
@ app.route(/ test,methods = [POST])
def test():$ b $ test = request.form [测试]
返回测试:%s%测试
@ app.route(/ index)
def index():
#用Flask中的something_like_this方法来执行POST请求?
return something_like_this(/ test,{test:My Test Data})
我没有在Flask文档中找到任何具体的东西。有人说 urllib2.urlopen
是问题,但是我没有把Flask和 urlopen
结合起来。真的有可能吗?
预先感谢!
解决方案
,要发出一个POST请求,你可以使用 urllib2
,参见文档。
编辑:
我建议你重构你的代码以提取常见的功能:
@ app.route(/ test,方法= [POST])
def test():
return _test(request.form [test])
@ app.route(/ index )
def index():
return _test(My Test Data)
def _test(argument):
returnTEST:%s%argument
There is a need to make POST request from server side in Flask.
Let's imagine that we have:
@app.route("/test", methods=["POST"])
def test():
test = request.form["test"]
return "TEST: %s" % test
@app.route("/index")
def index():
# Is there something_like_this method in Flask to perform the POST request?
return something_like_this("/test", { "test" : "My Test Data" })
I haven't found anything specific in Flask documentation. Some say urllib2.urlopen
is the issue but I failed to combine Flask and urlopen
. Is it really possible?
Thanks in advance!
解决方案
Yes, to make a POST request you can use urllib2
, see the documentation.
I would however recommend to use the requests module instead.
EDIT:
I suggest you refactor your code to extract the common functionality:
@app.route("/test", methods=["POST"])
def test():
return _test(request.form["test"])
@app.route("/index")
def index():
return _test("My Test Data")
def _test(argument):
return "TEST: %s" % argument
这篇关于是否有可能在Flask中发出POST请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文