url 解码 application/x-www-form-urlencoded 转字典 mysql存入json格式字符串

papering / 2023-06-07 / 原文

 

URL Decoding query strings or form parameters in Python | URLDecoder https://www.urldecoder.io/python/

 

flask接收 application/x-www-form-urlencoded格式数据,并转化为json格式字符串,写入mysql。

        """
        request.headers
        Content-Type', 'application/x-www-form-urlencoded'
        """
        try:
            import pprint, json
            ContentType = str(request.headers['Content-Type'])
            env = request.environ['REMOTE_ADDR'] + ' ' + request.environ['HTTP_USER_AGENT'] + ' ' + ContentType
            # 获取的数据,转字符串,This reads the buffered incoming data from the client into one
            #         bytestring.
            param0 = request.get_data(as_text=True)
            print("0=", param0)
            import urllib.parse
            # 转码为字典,同时encoding='utf-8'
            param1 = urllib.parse.parse_qs(param0)
            # 字典转json格式的字符串
            param2 = json.dumps(param1, ensure_ascii=False)
            print('2=', param2)
            sql = f'''INSERT INTO mydb.table (remark,remark2,env) VALUES ('{param2}','','{env}')'''