flask-restful出现字符串编码错误
flask-restful插件编写api时,会用到reqparse.RequestParser
模块,但是此模块在处理中文时会出现编码错误
比如代码如下:
1 2 3 4 5 6 |
self.parse = reqparse.RequestParser() self.parse.add_argument('domain', type=str, required=True, location='form') self.parse.add_argument('fromAttr', type=str, required=False, location='form') self.parse.add_argument('type', type=int, required=True, location='form') self.parse.add_argument('bestAttr', type=str, required=True, location='form') |
当请求中出现中文时会返回错误
{ "message": { "fromAttr": "'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)" } }
这是因为在add_argument模块中str表示接受unicode字符,并不是str字符,所以在使用str前,我会互换str和unicode
1 2 |
str, unicode = unicode, str |
链接:https://www.ioiogoo.cn/2016/12/14/flask-restful出现字符串编码错误/
本站所有文章除特殊说明外均为原创,转载请注明出处!