如何在烧瓶中路由之前修改request.args

时间:2018-10-16 11:19:44

标签: flask

我的加密网址如下:

  

http://host/interface/register?b0ba06b19a18a68394eb4a4eb6e7c2a2eaa0c25f0d42106efa05e382711bd5a83a49fe32b295057d4e6ea642563f9643a6bfa2c72f8333df865b47462c096980d5b4314f2d80dda11b850e5bc54899e06f7ac53856db88705b1cf9e087a0e11f5c0f1e62cc1e27285b1cf9e087a0e11fdba9c24d029d99d1e68c943145205c88b430ca6865528840065032b92f6b9d9eb5afc5fd5cb28e9aefec62361801f617

我想在路线中使用解码的args:

@app.route('/interface/register', methods=['GET', 'POST'])
def interface_register():
  args = request.args.to_dict()
  id = args.get('id', "")
  name = args.get('name', "")
  ...

所以我尝试在app.before_request中修改args:

@app.before_request
def before_request():
    # decode and modify request.args

由于request.args是只读的,因此不成功,因此如何在调用request.args.to_dict()?之前解码和修改原始request.args

1 个答案:

答案 0 :(得分:0)

from urllib.parse import parse_qs, urlparse

def your_decode(qs):
  # implement your decryption here
  return qs

url = 'http://blah/?id=1&y=[1,2]&blah=dd' # >> replace with request.url
qs = urlparse(url).query
decoded_qs = your_decode(qs)
o = parse_qs(decoded_qs)
print(o)

打印

{'blah': ['dd'], 'id': ['1'], 'y': ['[1,2]']}