Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Flask API TypeError:类型为';答复';JSON不可序列化_Python_Rest_Flask_Flask Restful - Fatal编程技术网

Python Flask API TypeError:类型为';答复';JSON不可序列化

Python Flask API TypeError:类型为';答复';JSON不可序列化,python,rest,flask,flask-restful,Python,Rest,Flask,Flask Restful,我对Python Flask Restful API有问题,数据转到Elasticsearch,当我向Postman发布新数据时,问题是: TypeError:类型为“Response”的对象不可JSON序列化 你能帮我吗 型号: from marshmallow import Schema, fields, validate class Person(object): def __init__(self,tcno=None,firstname=None,lastname=Non

我对Python Flask Restful API有问题,数据转到Elasticsearch,当我向Postman发布新数据时,问题是:

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

你能帮我吗

型号:

   from marshmallow import Schema, fields, validate

class Person(object):
    def __init__(self,tcno=None,firstname=None,lastname=None,email=None,birthday=None,country=None,gender=None):
        self.__tcno = tcno
        self.__firstname = firstname
        self.__lastname = lastname
        self.__email = email
        self.__birthday = birthday
        self.__country = country
        self.__gender = gender

    def __repr__(self):
        return '<Person(firstname={self.__firstname!r})>'.format(self=self)

class PersonSchema(Schema):
    tcno = fields.Str(required=True,validate=[validate.Length(min=11, max=11)])
    firstname = fields.Str(required=True)
    lastname = fields.Str(required=True)
    email = fields.Email(required=True,validate=validate.Email(error="Not a valid email"))
    birthday = fields.Date(required=True)
    country = fields.Str()
    gender = fields.Str()
编辑: 我发现了问题。这是在def post(self,people_id)方法中:

新行:

if errors:
    message = json.dumps({'errors': errors})
    return Response(message, status=422, mimetype='application/json')

这可以简单地通过以下方式实现:

from flask import jsonify
def myMethod():
    ....
    response = jsonify(data)
    response.status_code = 200 # or 400 or whatever
    return response
受此启发,以下是一个简短的方法:

from flask import jsonify, make_response

def myMethod():
    ....
    return make_response(jsonify(data), 200)

我有一个类似的问题,问题是我使用了两次jsonify(
jsonify(jsonify({abc:123}))


足以解决此问题

不要将
消息
包装在
响应中
。直接返回。如果我想要渲染模板paralalay retuen res,200,render_template()如何使用它
if errors:
    message = json.dumps({'errors': errors})
    return Response(message, status=422, mimetype='application/json')
from flask import jsonify
def myMethod():
    ....
    response = jsonify(data)
    response.status_code = 200 # or 400 or whatever
    return response
from flask import jsonify, make_response

def myMethod():
    ....
    return make_response(jsonify(data), 200)
def post(self):
    some_json=request.get_json()
    return jsonify({'you sent ':some_json})