【发布时间】:2020-01-01 10:21:56
【问题描述】:
我正在尝试测试一个函数,该函数基本上通过传递一些值来调用 API,然后将其加载到要验证的架构中,然后根据验证接受或拒绝。
下面是我的测试函数。
params = {
'first': [10,20],
'second': 400,
'third ': 'Testing'
}
headers = {
'Authorization': 'Bearer{}'.format(token),
'Data-type' : "json"
}
response = self.client.post(url_for('MyView:post', id=current_id.id),
json=params,
headers=headers)
这使我出现以下错误:
{
"msg": "Invalid header string: must be a json object"
}
我能够检查问题并发现它是由于无效类型的映射造成的。但由于 headers 仍然是一本字典,我无法理解为什么会呈现此错误。
我还在下面添加了我的架构和 API 的结构:
class MySchema(marshmallow.Schema):
id = fields.Integer(required=True)
second = fields.Integer(fields.Integer(), required=True)
first = fields.List(fields.String(), required=False)
third = fields.Str(validate=validate.Length(min=0, max=255), required=False)
class Meta:
fields = ('id',
'second',
'first',
'third')
@validates_schema(pass_original=True)
def validate_numbers(self, _, data):
//function code
下面是我的API的结构
class MyView(V1FlaskView):
@jwt_requried
def post(id):
//code
【问题讨论】:
-
你试过
"content-type":"application/json"吗? -
是的,我做到了。这没什么区别。仍然给我同样的错误。