Azure APIM:无需创建专属路由,全局策略暴露API文档可行性问询
Azure APIM全局策略批量暴露API文档问题
我在Azure APIM后端托管了多个API,计划以编程方式暴露API文档,因此有如下疑问:是否可以通过全局策略拦截指向../<api-service-id>/openapi.json的请求,无需为每个API创建专属路由即可返回对应端点的文档?
以下是最初编写但未生效的策略代码:
<policies> <inbound> <!--Dynamically call the APIM Management API--> <choose> <when condition="@(context.Request.OriginalUrl.Path.EndsWith("/openapi.json", StringComparison.OrdinalIgnoreCase))"> <send-request mode="new" response-variable-name="result" timeout="10" ignore-error="true"> <set-url>@("/subscriptions/my-sub/resourceGroups/my-reg/providers/Microsoft.ApiManagement/service/my-service" + "/apis/" + context.Request.MatchedParameters.GetValueOrDefault("api-id","") + "?export=true&format=openapi&api-version=2022-09-01-preview")</set-url> <set-method>GET</set-method> <authentication-managed-identity resource="https://management.azure.com/" /> </send-request> <!--Return the response--> <return-response> <set-status code="200" reason="OK" /> <set-header name="Content-Type" exists-action="override"> <value>application/json</value> </set-header> <set-body>@((string)(((IResponse)context.Variables["result"]).Body.As<JObject>()["value"]))</set-body> </return-response> </when> </choose> </inbound> <backend> <forward-request /> </backend> <outbound /> <on-error /> </policies>
编辑说明:在Ikhtesam Afrin的帮助下,代码已可正常运行,但仍未解决“需要为每个API创建端点”的核心问题,修改后的<set-body>代码片段如下:
<set-body>@(((IResponse)context.Variables["result"]).Body.As<JObject>().ToString())</set-body> </return-response>
内容的提问来源于stack exchange,提问作者Bennimi
相关产品推荐
相关产品推荐

