由于我已经擦除了VirtualEnv并安装了最新版本,因此单元测试不再通过。我很困惑,因为似乎Flask Restful似乎不再了解传入的json。
self.headers['Content-Type'] = 'application/json'
rv = self.client.post('api/v1.0/ftrecords',
headers=self.headers,
data=self.events_to_json([event1, event2]))
def events_to_json(self, events):
lst = []
for event in events:
lst.append(self.event_marshal(event))
return json.dumps({'events': lst})
def event_marshal(self, event):
evs = marshal(event, self.event_resource_fields)
evs.update({'event_id': event.key.id()})
return evs
self.event_resource_fields = {
'event_id': fields.String,
'time_of_day': fields.DateTime,
'rating': fields.Integer,
'location': fields.String,
'notes': fields.String,
'timestamp': fields.Integer,
'is_deleted': fields.Boolean,
'is_public': fields.Boolean
}
实际的API:
class FTEventAPI(Resource):
def __init__(self):
super(FTEventAPI, self).__init__()
self.reqparser = reqparse.RequestParser()
self.reqparser.add_argument('events', type=list, required=True)
def post(self):
arguments = self.reqparser.parse_args()
json_events = arguments.get('events’)
==>期待一个包含词典的列表
json_events ==> Thats wrong.
[u'rating', u'is_deleted', u'event_id', u'timestamp', u'notes', u'time_of_day', u'location', u'is_public’]
request.json.get('events') ==> This is correct
[{u'rating': 1, u'is_deleted': None, u'event_id': u'fc1f1a18-5e3a-4e5c-9610-269bc99da9ca', u'timestamp': 1416820073, u'notes': None, u'time_of_day': u'Mon, 24 Nov 2014 09:07:53 -0000', u'location': None, u'is_public': None},
{u'rating': 2, u'is_deleted': None, u'event_id': u'74698ff2-8af2-45a2-9e66-d0dff4e9986b', u'timestamp': 1416906473, u'notes': None, u'time_of_day': u'Tue, 25 Nov 2014 09:07:53 -0000', u'location': None, u'is_public': None}]
答案 0 :(得分:1)
我不确定发生了什么变化。但是,如果我添加location参数,它可以工作:
self.reqparser.add_argument('events', type=list, location='json', required=True)
这可能与此相同:
request.json.get('events')
我不确定,此代码之前是如何工作的,以及为什么它需要突然出现location='json'
参数。