TypeError: Object of type function is not JSON serializable when using flask_jwt_extended int RESTful API
TypeError: Object of type function is not JSON serializable when using flask_jwt_extended int RESTful API
我正在使用 Flask 构建 REST API。我正在使用 postman 测试在我的数据库中创建新项目的路由,但前提是用户已登录。注册和登录的路由运行良好,最后一个 returns使用 flask_jwt_extended 模块的令牌。当我向我的“/api/notes”发送 post 请求(在数据库中创建一个新笔记)时,我收到以下错误消息:
" (...) raise TypeError(f'Object of type {o.class.name} '
TypeError:类型函数的对象不是JSON可序列化的
对于请求,我正在使用 postman 的授权选项卡。类型:Bearer Token,字段中是我的令牌(带引号和不带引号都试过了)
今天早上我遇到了这个错误,在实施我的一对多关系之前,但我通过在 Barear 令牌字段中将我的 VERY_LONG_TOKEN 替换为“VERY_LONG_TOKEN” 来让它工作。我认为因为令牌包含点,所以它被解释为一个函数。但是实现关系后,我去测试,又报这个错
我的note.py文件:
from flask import request, Response, jsonify
from app.models import User, Note
from flask_restful import Resource
from flask_jwt_extended import jwt_required, get_jwt_identity
class NotesApi(Resource):
def get(self):
notes = Note.objects().to_json()
return Response(notes, mimetype="application/json", status=200)
@jwt_required
def post(self): # post method I'm making a request for
print("fool") # this doesn't get printed -> not reaching
user_id = get_jwt_identity()
data = request.get_json(force=True)
if data:
user = User.objects(id=user_id) # logged in user
note = Note(**data, user_author=user) # creates note with the author
note.save()
user.update(push__notes=note) # add this note to users notes
user.save()
id = str(note.id)
return {'id': id}, 200
else:
return {'error': 'missing data'}, 400
我的models.py:
from app import db # using mongodb
from datetime import datetime
from flask_bcrypt import generate_password_hash, check_password_hash
class Note(db.Document):
title = db.StringField(max_length=120,required=True)
content = db.StringField(required=True)
status = db.BooleanField(required=True, default=False)
date_modified = db.DateTimeField(default=datetime.utcnow)
user_author = db.ReferenceField('User')
class User(db.Document):
username = db.StringField(max_length=100, required=True, unique=True)
email = db.StringField(max_length=120, required=True, unique=True)
password = db.StringField(required=True)
remember_me = db.BooleanField(default=False)
notes = db.ListField(db.ReferenceField('Note', reverse_delete_rule=db.PULL)) # one-many relationship
def hash_password(self):
self.password = generate_password_hash(self.password).decode('utf8')
def check_password(self, password):
return check_password_hash(self.password, password)
User.register_delete_rule(Note, 'user_author', db.CASCADE)
初始化.py:
from flask import Flask
from config import Config # my config class to set MONGOBD_HOST and SECRET_CLASS
from flask_mongoengine import MongoEngine
from flask_restful import Api
from flask_bcrypt import Bcrypt
from flask_jwt_extended import JWTManager
app = Flask(__name__)
app.config.from_object(Config)
db = MongoEngine(app)
api = Api(app)
bcrypt = Bcrypt(app)
jwt = JWTManager(app)
from app.resources.routes import initialize_routes
initialize_routes(api)
resources/routes.py:
from .note import NotesApi, NoteApi
from .auth import SignupApi, LoginApi
def initialize_routes(api):
api.add_resource(NotesApi, '/api/notes')
api.add_resource(NoteApi, '/api/note/<id>')
api.add_resource(SignupApi, '/api/auth/signup')
api.add_resource(LoginApi, '/api/auth/login')
文件夹结构:
app
|_ resources
|_ auth.py # signup working well, login also working, return a token (type = String)
|_ note.py
|_ routes.py
|_ __init__.py
|_ models.py
config.py
appname.py #just import app and do a app.run()
我的 post 请求正文:
{
"title": "test0",
"content": "test0"
}
有没有人遇到过或知道如何解决?
编辑:添加了更多代码信息
看起来 flask-jwt-extended 在周末发布了一个新版本。作为 API 更改的一部分,@jwt_required
装饰器现在是 @jwt_required()
https://flask-jwt-extended.readthedocs.io/en/stable/v4_upgrade_guide/#api-changes
去掉对我有用的 HTTP 状态代码。示例
替换:
return {'id': id}, 200
与:
return {'id': id}
或:
resp = jsonify({'id': id})
resp.status_code = 200
return resp
我正在使用 Flask 构建 REST API。我正在使用 postman 测试在我的数据库中创建新项目的路由,但前提是用户已登录。注册和登录的路由运行良好,最后一个 returns使用 flask_jwt_extended 模块的令牌。当我向我的“/api/notes”发送 post 请求(在数据库中创建一个新笔记)时,我收到以下错误消息:
" (...) raise TypeError(f'Object of type {o.class.name} '
TypeError:类型函数的对象不是JSON可序列化的
对于请求,我正在使用 postman 的授权选项卡。类型:Bearer Token,字段中是我的令牌(带引号和不带引号都试过了)
今天早上我遇到了这个错误,在实施我的一对多关系之前,但我通过在 Barear 令牌字段中将我的 VERY_LONG_TOKEN 替换为“VERY_LONG_TOKEN” 来让它工作。我认为因为令牌包含点,所以它被解释为一个函数。但是实现关系后,我去测试,又报这个错
我的note.py文件:
from flask import request, Response, jsonify
from app.models import User, Note
from flask_restful import Resource
from flask_jwt_extended import jwt_required, get_jwt_identity
class NotesApi(Resource):
def get(self):
notes = Note.objects().to_json()
return Response(notes, mimetype="application/json", status=200)
@jwt_required
def post(self): # post method I'm making a request for
print("fool") # this doesn't get printed -> not reaching
user_id = get_jwt_identity()
data = request.get_json(force=True)
if data:
user = User.objects(id=user_id) # logged in user
note = Note(**data, user_author=user) # creates note with the author
note.save()
user.update(push__notes=note) # add this note to users notes
user.save()
id = str(note.id)
return {'id': id}, 200
else:
return {'error': 'missing data'}, 400
我的models.py:
from app import db # using mongodb
from datetime import datetime
from flask_bcrypt import generate_password_hash, check_password_hash
class Note(db.Document):
title = db.StringField(max_length=120,required=True)
content = db.StringField(required=True)
status = db.BooleanField(required=True, default=False)
date_modified = db.DateTimeField(default=datetime.utcnow)
user_author = db.ReferenceField('User')
class User(db.Document):
username = db.StringField(max_length=100, required=True, unique=True)
email = db.StringField(max_length=120, required=True, unique=True)
password = db.StringField(required=True)
remember_me = db.BooleanField(default=False)
notes = db.ListField(db.ReferenceField('Note', reverse_delete_rule=db.PULL)) # one-many relationship
def hash_password(self):
self.password = generate_password_hash(self.password).decode('utf8')
def check_password(self, password):
return check_password_hash(self.password, password)
User.register_delete_rule(Note, 'user_author', db.CASCADE)
初始化.py:
from flask import Flask
from config import Config # my config class to set MONGOBD_HOST and SECRET_CLASS
from flask_mongoengine import MongoEngine
from flask_restful import Api
from flask_bcrypt import Bcrypt
from flask_jwt_extended import JWTManager
app = Flask(__name__)
app.config.from_object(Config)
db = MongoEngine(app)
api = Api(app)
bcrypt = Bcrypt(app)
jwt = JWTManager(app)
from app.resources.routes import initialize_routes
initialize_routes(api)
resources/routes.py:
from .note import NotesApi, NoteApi
from .auth import SignupApi, LoginApi
def initialize_routes(api):
api.add_resource(NotesApi, '/api/notes')
api.add_resource(NoteApi, '/api/note/<id>')
api.add_resource(SignupApi, '/api/auth/signup')
api.add_resource(LoginApi, '/api/auth/login')
文件夹结构:
app
|_ resources
|_ auth.py # signup working well, login also working, return a token (type = String)
|_ note.py
|_ routes.py
|_ __init__.py
|_ models.py
config.py
appname.py #just import app and do a app.run()
我的 post 请求正文:
{
"title": "test0",
"content": "test0"
}
有没有人遇到过或知道如何解决?
编辑:添加了更多代码信息
看起来 flask-jwt-extended 在周末发布了一个新版本。作为 API 更改的一部分,@jwt_required
装饰器现在是 @jwt_required()
https://flask-jwt-extended.readthedocs.io/en/stable/v4_upgrade_guide/#api-changes
去掉对我有用的 HTTP 状态代码。示例
替换:
return {'id': id}, 200
与:
return {'id': id}
或:
resp = jsonify({'id': id})
resp.status_code = 200
return resp