gpt4 book ai didi

Python:jsonify 中的 str(bytes object) 导致 TypeError: 类型 'bytes' 的对象不可 JSON 序列化

转载 作者:行者123 更新时间:2023-12-02 02:47:04 26 4
gpt4 key购买 nike

我向 API 发送了一个请求,但它抛出 TypeError:“bytes”类型的对象不可 JSON 序列化。它还返回 500 INTERNAL SERVER ERROR。

代码:

def login():
data = request.json
if(data['token'] != 'xxxxxxxxxxx'):
return jsonify(), 401
user = User.objects(npm=data['npm']).first()
if(user == None):
del data['token']
user = User(**data)
user.major = Major.objects(name=data['major']).first()
user.role = 'user'
user.save()
token = jwt.encode({
'user_id': str(user.id)
}, secret_key, algorithm='HS256')
return jsonify({
'user_id': str(user.id),
'token': token,
'major_id': str(user.major.id)
}), 200

回溯:

Traceback (most recent call last):
File "c:\users\anisha\env\lib\site-packages\flask\app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "c:\users\anisha\env\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "c:\users\anisha\env\lib\site-packages\flask_cors\extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "c:\users\anisha\env\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "c:\users\anisha\env\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "c:\users\anisha\env\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "c:\users\anisha\env\lib\site-packages\flask\app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "c:\users\anisha\env\lib\site-packages\flask_cors\decorator.py", line 128, in wrapped_function
resp = make_response(f(*args, **kwargs))
File "C:\Users\Anisha\sunjadv2-server\app.py", line 69, in login
'major_id': str(user.major.id)
File "c:\users\anisha\env\lib\site-packages\flask\json\__init__.py", line 321, in jsonify
dumps(data, indent=indent, separators=separators) + '\n',
File "c:\users\anisha\env\lib\site-packages\flask\json\__init__.py", line 179, in dumps
rv = _json.dumps(obj, **kwargs)
File "C:\Users\Anisha\AppData\Local\Programs\Python\Python36\lib\json\__init__.py", line 238, in dumps
**kw).encode(obj)
File "C:\Users\Anisha\AppData\Local\Programs\Python\Python36\lib\json\encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:\Users\Anisha\AppData\Local\Programs\Python\Python36\lib\json\encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "c:\users\anisha\env\lib\site-packages\flask\json\__init__.py", line 81, in default
return _json.JSONEncoder.default(self, o)
File "C:\Users\Anisha\AppData\Local\Programs\Python\Python36\lib\json\encoder.py", line 180, in default
o.__class__.__name__)
TypeError: Object of type 'bytes' is not JSON serializable

当我执行 print("major_id: "+ str(user.major.id)) 时,它将打印 ma​​jor_id: 5b55e986f15bf336e0b820fe 到控制台。为什么 str(user.major.id) 在 jsonify 中被视为 bytes 类型?我尝试删除 'major_id': str(user.major.id) 但行 'token': token 将导致相同的错误。

谢谢。

最佳答案

您的数据似乎无法正确解码。您可以尝试在 jsonify 之前对数据进行解码。

例如,不要直接使用 token ,而是尝试使用:token.decode('utf-8')

关于Python:jsonify 中的 str(bytes object) 导致 TypeError: 类型 'bytes' 的对象不可 JSON 序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51481755/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com