【问题标题】:Flask server side form POSTFlask 服务器端表单 POST
【发布时间】:2014-12-24 16:17:55
【问题描述】:

我正在尝试在 Flask 上模拟服务器端表单发布操作。 通过使用 Requests lib 我做后期操作:

@flask_app.route('/hello',methods=['GET','POST'])
def hello():
    r   = requests.post(              
             "https://somesite.hello"
            , data= {'user': 'bill','pass': 123})
    return r.text                             
    #return redirect("https://somesite.hello")

而且它的工作。但是当我通过 html 发布真实表单时,它也会进行重定向。 #1 之后的 url 将是 http://mysite.hello:5000/hello,但我需要来自 #1 的内容,但需要来自 #2 https://somesite.hello 的 url 如何同时执行 1 和 2?谢谢!

更新:

我找到了一些解决方案,我不太喜欢它,因为用户会看到“重定向到计费页面”页面一秒钟。可能有人知道如何请求和重定向以获得一些结果?

@flask_app.route('/hello',methods=['GET','POST'])
def hello():
    url = "https://somesite.hello"
    return flask_app.make_response("""
    <html>
        <body onload='document.forms[0].submit()'>
            Redirecting to billing page...
            <form action='%s' method='post'>
                <input type='hidden' name='user' value='%s'>
                <input type='hidden' name='pass' value='%s'>
            </form>
        </body>
    </html>
    """%(url
        ,'bill'
        ,123)) 

【问题讨论】:

  • 这可能会有所帮助:stackoverflow.com/questions/17057191/…
  • 那你打算怎么用r.text呢?
  • 我的烧瓶应用程序正在与计费站点通信。要下订单,您必须使用一些数据(shop_id、pass_id、sign)制作第一个 html 表单,并且当您按下提交按钮时(这是我尝试通过烧瓶执行的操作)重定向发生在您所在的 sec 表单可以写卡信息并提交支付。我试着只用一步来做。此 r.text 是计费站点上的第二种形式,但 windows.location 是我的站点。我不知道如何通过烧瓶更改 windows.location。只能通过 jscript 吗?

标签: python flask


【解决方案1】:

不要跟随重定向并发出您自己的重定向到以下页面:

r = requests.post('some.url', data={'some': 'data'}, allow_redirects=False)
return redirect(r.headers['location'])

我们提交表单数据,但不遵循请求的重定向。相反,我们将重定向传递给最终用户。但是,如果支付处理器使用 cookie 来保存状态(它很可能会这样做),这将不起作用,因为 some.url 为您的请求设置的 cookie 无法为 some.url 传递。

【讨论】:

  • 感谢您的回答,但 its dont 不会处理我的帐单。我不确定为什么。我找到了一个解决方案,我会在这里发布,但它更像是诡计。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-09
  • 1970-01-01
  • 2021-11-23
  • 2015-03-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-24
相关资源
最近更新 更多