TypeError:使用flask_jwt_extended int RESTful API时,类型函数的对象不可JSON序列化

威廉·苏扎

我正在使用flask构建REST API。我正在使用邮递员测试在我的数据库中创建新项目的路由,但前提是用户已登录。注册和登录的路由运行良好,最后一条使用flask_jwt_extended模块返回了令牌。当我向“ / api / notes”发送发布请求(在数据库中创建新的注释)时,出现以下错误:

“(...)升类型错误(f'Object类型{O操作。}”

TypeError:类型函数的对象不可JSON序列化”

对于请求,我使用的是邮递员的授权标签。类型:不记名令牌,以及我在字段中的令牌(尝试使用引号和不使用引号)

在实施我的一对多配售计划之前,我今天早上遇到了这个错误,但是通过在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) 

资源/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()

我的职位要求正文:

{
   "title": "test0",
   "content": "test0"  
}

有没有人以前遇到过它或知道如何解决它?

编辑:添加了更多代码信息

Maylor

好像flask-jtw-extended在周末发布了新版本。作为API更改的一部分,@jwt_required装饰器现在@jtw_required()

https://flask-jwt-extended.readthedocs.io/en/stable/v4_upgrade_guide/#api-changes

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

TypeError:“ DataFrame”类型的对象不可JSON序列化

使用Flask创建RESTful API?

使用ChoiceField时类型错误对象不可JSON序列化

TypeError:“标记”类型的对象不可JSON序列化

在flask-restful和create_app中使用flask-jwt-extended回调

TypeError:“ int32”类型的对象不可JSON序列化

TypeError:TextIOWrapper类型的对象不可JSON序列化

TypeError:类型X的对象不可JSON序列化

flask_jwt_extended在解码我的JWT时引发错误。我该如何捕捉呢?

序列化程序调用显示TypeError:类型为“ ListSerializer”的对象不可JSON序列化?

TypeError:类型为date的对象不可JSON序列化

“ TypeError:字节类型的对象不可JSON序列化”

flask-jwt-extended TypeError:类型为'function'的对象不可JSON序列化

TypeError:“ datetime”类型的对象不可JSON序列化(带有序列化功能)

TypeError:对象不可JSON序列化

TypeError:类型函数的对象不可JSON序列化

TypeError:类型的对象不可JSON序列化

TypeError:int64类型的对象不可JSON序列化

flask_restful-TypeError:Record类型的对象不是JSON可序列化的

当我使用fastapi和pydantic构建POST API时,出现TypeError:类型为JSON的对象无法序列化

TypeError:使用请求时类型集的对象不可JSON序列化

序列化模型数据会导致TypeError“ ListSerializer类型的对象不可JSON序列化”

TypeError:比较request.form.get和db值时,Cursor类型的对象不可JSON序列化

气流DAG序列化:TypeError:类型为“ V1Pod”的对象不可JSON序列化

TypeError:Series类型的对象不可JSON序列化

Django:TypeError:ModelBase类型的对象不可JSON序列化

Restful Web Api参数作为int数组

Python Flask API Restful

使用 Bearer Token Authorization Header (flask_restful + flask_jwt_extended) 发送 GET 消息时出现“没有足够的段”