简体   繁体   English

如何在WSO2 API管理器中忽略具有无效json主体的API调用

[英]How to Ignore API call with invalid json body in WSO2 API manager

I use wso2 API manager for publishing some of my APIs. 我使用wso2 API管理器发布一些API。 But my issue is if someone send a API request with invalid json body, is it able to ignore that API call and give some error message to the requested person without sending that invalid json body to the back end. 但是我的问题是,如果有人发送带有无效json主体的API请求,它是否能够忽略该API调用并向被请求的人提供一些错误消息,而无需将该无效的json主体发送给后端。 API manager version : 1.7 API管理器版本:1.7

  • This is the incorrect json request (try to send without value for "moduleCode") 这是不正确的json请求(尝试发送“ moduleCode”不带任何值)

     { "event": { "portalType": "dfd", "moduleCode": , "role": "3", "roleCode": "dfdf", "languageID": "gd", "password": "dfdfgd", "phoneNumber": "2332323", "resourceCode": [ "8045005" ] }} 

Could it possible to restrict user by value of role in the above json ? 是否可以通过上述json中的角色值限制用户? How to achieve this with API Manager 2.0 ? 如何使用API​​ Manager 2.0实现这一目标?

Thre aren't any predefined way to achieve this requirement. 尚没有任何预定义的方法可以达到此要求。 But, You can customize WSO2 API Manager to cater this requirement. 但是,您可以自定义WSO2 API管理器来满足此要求。

You can write a custom handler which will validate the request body and return response if the request has some error rather sending to backend. 您可以编写一个自定义处理程序,该处理程序将验证请求正文并在请求有错误时返回响应,而不是发送给后端。 You can check here on how to write a custom handler for API Manager 2.0. 您可以在此处查看有关如何为API Manager 2.0编写自定义处理程序的信息。

You can refer this article on "How to validate JSON request payload in API Manager" . 您可以参考“如何在API Manager中验证JSON请求有效负载”这篇文章。 This is exactly address your use case and only thing you need to do is add XSD to match with your request. 这恰好解决了您的用例,您唯一需要做的就是添加XSD以符合您的请求。 In API Manager if you need to validate request or pre process message its always recommend to use mediation extensions available with the product. 在API Manager中,如果您需要验证请求或预处理消息,则始终建议使用产品随附的中介扩展。

In coming request will be transform to SOAP format by message builder[1].This message builder will be chosen based on the content type of incoming message.You can write your own message builder for application/json and validate the input. 在即将到来的请求中,消息构建器[1]会将其转换为SOAP格式。将根据传入消息的内容类型选择此消息构建器。您可以为application / json编写自己的消息构建器并验证输入。 eg [2] 例如[2]

[1] https://docs.wso2.com/display/ESB481/Working+with+Message+Builders+and+Formatters [2] https://github.com/anuruddhal/WSO2-ESB-Custom-Message-Builder/tree/master/CustomBuilder [1] https://docs.wso2.com/display/ESB481/Working+with+Message+Builders+and+Formatters [2] https://github.com/anuruddhal/WSO2-ESB-Custom-Message-Builder /树/主/ CustomBuilder

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM