标题值:制作“应用程序/vnd.api”
我正在Python开发一个Restfull Jsonapi,并通过棉花糖实施了验证。
我正在使用两个库的组合 safrs 和烧瓶式jsonapi 。我添加了第二个库,以包括棉花糖验证,因为库SAFRS没有。我遵循此 example 。
验证是通过模式来定义的:
class UserSchema(Schema):
class Meta:
type_ = 'user'
self_view = 'user_detail'
self_view_kwargs = {'id': '<id>'}
self_view_many = 'user_list'
id = fields.Integer(as_string=True, dump_only=True)
user_name = fields.Str(required=True, attribute='username', allow_none=False)
password = fields.Str(required=True, allow_none=False)
display_name = fields.Function(lambda obj: "{} <{}>".format(obj.username.upper(), obj.username))
我发现,只有当标头的内容类型为application/vnd.api+json
时,验证才有效。 问题在于,也接受了标题application/json
下的请求,在这种情况下,验证将被忽略。
因此,是否有一种方法可以使application/vnd.api+json
强制性?
I'm developing a restfull jsonapi in python and implemented a validation through marshmallow.
I'm using a combination of two libraries SAFRS and flask-rest-jsonapi. I add the second library to include a marshmallow validation, as the library SAFRS doesn't have it. I followed this example.
The validation is defined through schemas:
class UserSchema(Schema):
class Meta:
type_ = 'user'
self_view = 'user_detail'
self_view_kwargs = {'id': '<id>'}
self_view_many = 'user_list'
id = fields.Integer(as_string=True, dump_only=True)
user_name = fields.Str(required=True, attribute='username', allow_none=False)
password = fields.Str(required=True, allow_none=False)
display_name = fields.Function(lambda obj: "{} <{}>".format(obj.username.upper(), obj.username))
I found out, that the validation is only effective if the content-type of the header is application/vnd.api+json
.
The problem is that requests under the header application/json
are also accepted, and in this case, the validation is ignored.
Therefore, is there be a way to make application/vnd.api+json
mandatory?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找到了解决问题的不同方法。一种方法是通过中间件,但是那里的错误处理不是最佳的。最后,我选择了这个:
Found different ways of fixing the problem. One way would be via middleware, but the error handling there is not optimal. At the end I chose this one: