自定义对无效API操作的响应 [英] Customize Response to Invalid API Operation

查看:50
本文介绍了自定义对无效API操作的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用无效的API操作调用API时,如何更改Azure API Management(APIM)中的标准响应


用于  实例


如果我调用API中未定义的操作,我会收到"HTTP错误404资源未找到"


如果我打电话一个已定义的操作,但使用了错误的方法,比如一个应该使用GET的POST我得到一些其他HTTP错误响应。


以下是一些可能的错误示例


" HTTP Error 404 Resource Not Found"如果POST而不是GET


" HTTP Error 411.请求必须被分块或具有内容长度"当PUT而不是Get。


我想更改为在对调用者的响应中使用JSON的标准响应,但这些错误似乎没有达到APIM策略引擎,因此策略中寻找错误的任何代码似乎都没有触发。这几乎就像APIM在将呼叫传递给策略引擎之前检查
的有效性,如果它无效,它只是立即响应。


提前致谢。  ; 

解决方案

嗨  TheR00nster 
还没有尝试过你的确切方案,但我想  < on-error>  政策没有抓住这个?


更新:它似乎不是受支持的方案,因为它目前正在审核中:  ; https://feedback.azure.com/forums/248703 -api-management / suggestions / 7309609-customize-error-schema-messages  



请考虑根据需要更新此现有请求。


How can I change the standard response(s) in Azure API Management (APIM) when the API is called using an invalid API operation

For instance

If I call an operation that isn't defined in the API I get a "HTTP Error 404 Resource Not Found"

If I call an operation that is defined but use the wrong method, say a POST when it should have used a GET I get some other HTTP error response.

Here are some examples of possible errors

"HTTP Error 404 Resource Not Found" if POST instead of GET

"HTTP Error 411. The request must be chunked or have a content length" when PUT instead of Get.

I'd like to change to standard responses to using a JSON in the response to the caller but these errors don't seem to get as far as the APIM Policy Engine so any code in policies looking for an error doesn't seem to trigger. It's almost like APIM checks the validity of the call before passing it to the policy engine and if it isn't valid it just responds straight away.

Thanks in advance.  

解决方案

Hi TheR00nster I haven't tried it on your exact scenario yet but i guess the <on-error> policy isn't capturing this?

UPDATE: It doesn't appear to be a supported scenario as it is currently under review: https://feedback.azure.com/forums/248703-api-management/suggestions/7309609-customize-error-schema-messages 

Please consider upvoting this existing request as needed.


这篇关于自定义对无效API操作的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆