2016-08-03 72 views
0

使用Routes庫時,我想要redirect某些URL。該文件說,它可以這樣做:在Python中映射URL參數請求重定向

map.redirect("/legacyapp/archives/{url:.*}", "/archives/{url}") 

而且我確實能夠重定向到一個URL這種方式。但是,我無法將請求中的URL參數映射/解析爲重定向。我的代碼如下所示:

app.mapper.redirect("/repository/status_for_installed_repository{url:.*}", "/api/repositories/check_updates/{url}") 

,如果應用程序是通過這樣的:

curl -L 'FQDN/repository/status_for_installed_repository?owner=qqqqqq&changeset_revision=e5f6ced3e91f&name=asdsadsadas' 

它重定向我

GET /api/repositories/check_updates 

,但我不能找到一種方法如何獲取值ownernamechangeset_revision

我希望這是一個常見的用例,因爲通常在重定向時不想失去參數?

任何幫助,非常感謝。謝謝。

回答

0

我最終實現它,如下所示:

def _map_redirects(app): 
    """ 
    Add redirect to the Routes mapper and forward the received query string. 
    Subsequently when the redirect is triggered in Routes middleware the request 
    will not even reach the webapp. 
    """ 
    def forward_qs(environ, result): 
     qs_dict = urlparse.parse_qs(environ['QUERY_STRING']) 
     for qs in qs_dict: 
      result[ qs ] = qs_dict[ qs ] 
     return True 

    app.mapper.redirect("/repository/status_for_installed_repository", "/api/repositories/check_updates/", _redirect_code="301 Moved Permanently", conditions=dict(function=forward_qs)) 
    return app