如何使用flask.redirect发送POST请求?

时间:2019-02-14 12:58:21

标签: python redirect post flask

我正在尝试创建一个Flask服务,在其中我想将来自一个request.form的数据发送到json格式的另一个url,请问有人可以帮助我实现这一点吗?

redirect(url_for('any_method'), json = json.dumps(my_form_dict))

当我尝试执行以上代码时,出现以下错误:

TypeError: redirect() got an unexpected keyword argument 'json' The above is the error here.

3 个答案:

答案 0 :(得分:3)

您可以使用307状态代码重定向POST请求。 使用:

redirect(url_for('any_method', json=json.dumps(my_form_dict)), code=307)

有关更多信息,请参考以下答案: Make a POST request while redirecting in flask

答案 1 :(得分:0)

您的问题是您向redirect函数传递了太多参数。它仅需要三个参数locationcodeResponse。如果要传递其他参数,请使用Flask url_for方法:

redirect(url_for('any_method', json=form_json))

请注意区别,您将url_for和extra字段作为两个参数传递。在我的版本中,我在url_for中添加了额外的字段,因此redirect仅接收一个参数。

答案 2 :(得分:0)

无法重定向POST请求。 更多信息是here

相关问题