Azure Api 管理 returns 支持关闭时的详细错误包括誓言令牌
Azure Api Management returns detail error when backed is off include oath token
我正在处理 API 管理,后端使用 oauth 客户端凭据进行保护。如果后端关闭,我会得到 200 的响应和如下详细错误。
{
"error": {
"name": "StatusCodeError",
"statusCode": 404,
"message": "HTTP Error 404. The requested resource is not found.",
"options": {
"url": "https://...net.au/api/case/mycases/",
"method": "GET",
"headers": {
"Authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhb....."
},
"simple": true,
"resolveWithFullResponse": false,
"transform2xxOnly": false
},
"response": {
"statusCode": 404,
"body": "HTTP Error 404. The requested resource is not found.",
"headers": {
"content-length": "315",
"content-type": "text/html; charset=us-ascii",
"server": "Microsoft-HTTPAPI/2.0 Microsoft-HTTPAPI/2.0",
"date": "Fri, 14 Jun 2019 02:12:36 GMT",
"connection": "close"
},
"request": {
"uri": {
"protocol": "https:",
"slashes": true,
"auth": null,
"host": ".....",
"port": 443,
"hostname": "....net.au",
"hash": null,
"search": null,
"query": null,
"pathname": "/api/case/mycases/",
"path": "/api/case/mycases/",
"href": "https://...."
},
"method": "GET",
"headers": {
"Authorization": "Bearer eyJ0eXAiO....."
}
}
}
},
"status": 501
}
我只想 return 调用 api 时那样的响应。并隐藏所有额外的详细信息,包括访问令牌。
{
"error": {
"name": "StatusCodeError",
"statusCode": 404,
"message": "HTTP Error 404. The requested resource is not found.",
}
}
根据下面的回答,我已经更新了我的政策,当后端离线时我得到了想要的响应,但当后端在线时我得到了空响应。
<choose>
<when condition="@{
var token = context.Response.Body.As<JToken>();
if (token is JObject){
return true;
}
return false;
}">
<return-response>
<set-status code="404" reason="NotFound" />
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body>{
"error": {
"name": "StatusCodeError",
"statusCode": 404,
"message": "HTTP Error 404. The requested resource is not found.",
}
}</set-body>
</return-response>
</when>
</choose>
有两种情况可以从 APIM 获得 404 响应。一种是当您尝试调用 APIM 不知道的 API/operation 时,在这种情况下,APIM 将生成 404 响应并且它会非常短,就像您的第二个响应一样。
另一种情况是当 APIM 识别 API/operation 时,调用后端并且后端响应 404。在这种情况下,APIM 不会将此视为问题,而只是将后端响应中继给客户端。您在第一个示例中拥有的内容看起来像是后端会回复的内容。您可以通过从 Azure 门户中的测试控制台进行调用并检查提供的跟踪来确认这一点。
所以您要做的是用您选择的一种替换 404 响应的正文,这可以通过策略轻松完成。在任何范围内按照这些行放置内容:
<outbound>
<base/>
<choose>
<when condition="@(context.Response.StatusCode == 404)">
<return-response>
<set-status code="404" reason="NotFound">
<set-header name="Content-Type">
<value>application/json</value>
</set-header>
<set-body>{
"error": {
"name": "StatusCodeError",
"statusCode": 404,
"message": "HTTP Error 404. The requested resource is not found.",
}
}</set-body>
</return-response>
</when>
</choose>
</outbound>
我正在处理 API 管理,后端使用 oauth 客户端凭据进行保护。如果后端关闭,我会得到 200 的响应和如下详细错误。
{
"error": {
"name": "StatusCodeError",
"statusCode": 404,
"message": "HTTP Error 404. The requested resource is not found.",
"options": {
"url": "https://...net.au/api/case/mycases/",
"method": "GET",
"headers": {
"Authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhb....."
},
"simple": true,
"resolveWithFullResponse": false,
"transform2xxOnly": false
},
"response": {
"statusCode": 404,
"body": "HTTP Error 404. The requested resource is not found.",
"headers": {
"content-length": "315",
"content-type": "text/html; charset=us-ascii",
"server": "Microsoft-HTTPAPI/2.0 Microsoft-HTTPAPI/2.0",
"date": "Fri, 14 Jun 2019 02:12:36 GMT",
"connection": "close"
},
"request": {
"uri": {
"protocol": "https:",
"slashes": true,
"auth": null,
"host": ".....",
"port": 443,
"hostname": "....net.au",
"hash": null,
"search": null,
"query": null,
"pathname": "/api/case/mycases/",
"path": "/api/case/mycases/",
"href": "https://...."
},
"method": "GET",
"headers": {
"Authorization": "Bearer eyJ0eXAiO....."
}
}
}
},
"status": 501
}
我只想 return 调用 api 时那样的响应。并隐藏所有额外的详细信息,包括访问令牌。
{
"error": {
"name": "StatusCodeError",
"statusCode": 404,
"message": "HTTP Error 404. The requested resource is not found.",
}
}
根据下面的回答,我已经更新了我的政策,当后端离线时我得到了想要的响应,但当后端在线时我得到了空响应。
<choose>
<when condition="@{
var token = context.Response.Body.As<JToken>();
if (token is JObject){
return true;
}
return false;
}">
<return-response>
<set-status code="404" reason="NotFound" />
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body>{
"error": {
"name": "StatusCodeError",
"statusCode": 404,
"message": "HTTP Error 404. The requested resource is not found.",
}
}</set-body>
</return-response>
</when>
</choose>
有两种情况可以从 APIM 获得 404 响应。一种是当您尝试调用 APIM 不知道的 API/operation 时,在这种情况下,APIM 将生成 404 响应并且它会非常短,就像您的第二个响应一样。
另一种情况是当 APIM 识别 API/operation 时,调用后端并且后端响应 404。在这种情况下,APIM 不会将此视为问题,而只是将后端响应中继给客户端。您在第一个示例中拥有的内容看起来像是后端会回复的内容。您可以通过从 Azure 门户中的测试控制台进行调用并检查提供的跟踪来确认这一点。
所以您要做的是用您选择的一种替换 404 响应的正文,这可以通过策略轻松完成。在任何范围内按照这些行放置内容:
<outbound>
<base/>
<choose>
<when condition="@(context.Response.StatusCode == 404)">
<return-response>
<set-status code="404" reason="NotFound">
<set-header name="Content-Type">
<value>application/json</value>
</set-header>
<set-body>{
"error": {
"name": "StatusCodeError",
"statusCode": 404,
"message": "HTTP Error 404. The requested resource is not found.",
}
}</set-body>
</return-response>
</when>
</choose>
</outbound>