mercredi 28 août 2019

JWS Error during API testing python flask

I am trying to test a function, which basically calls the API by passing a some values, which is then loaded to the schema to be validated and then accepted or rejected based on validation.

The below is my test function.

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)

This renders me the error below:

{
  "msg": "Invalid header string: must be a json object"
}

I was able to check the issue and found out its due to the mapping of invalid types. But as headers is still a dictionary, I am not able to comprehend why this error is being rendered.

I have also added the structure of my schema and API below:

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

The below is the structure of my API

 class MyView(V1FlaskView):
   @jwt_requried
   def post(id):
      //code

Aucun commentaire:

Enregistrer un commentaire