azure - APIM 从现有资源生成 API 不断返回 200

标签 azure azure-api-management

我正在尝试让 API 管理转发请求并使用模板给我响应 - 从现有应用服务创建

我选择我的应用服务,所有操作都生成正常

但是,无论我做什么,每当我调用新 API 的任何端点时,我都会收到完全无用的 200 响应

请有人帮忙,因为这现在真的让我很沮丧

<policies>
    <inbound>
        <base />
        <rewrite-uri template="@(context.Request.Url.Path.Replace("/pml","/api"))" />
        <set-backend-service id="apim-generated-policy" backend-id="myresource" />
    </inbound>
    <backend>
        <forward-request />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

保罗

最佳答案

1.如果您尝试正确设置 APIM 转发到真实的应用服务:

将 APIM 策略中的基本 URL 设置为指向您的应用服务 DNS 或内部 IP(如果可从 APIM 访问):

<set-backend-service base-url="https://yourserviceIP" />
<set-backend-service base-url="https://myservice.azurewebsites.net" />

JWT 示例:

<policies>
    <inbound>
        <validate-jwt header-name="Authorization" failed-validation-httpcode="401" failed-validation-error-message="Unauthorized. Access token is missing or invalid.">
            <openid-config url="https://login.microsoftonline.com/yourtenant/.well-known/openid-configuration" />
            <audiences>
                <audience>[AppAudience]</audience>
            </audiences>
            <issuers>
                <issuer>[AppIssuer]</issuer>
            </issuers>
        </validate-jwt>
        <base />
        <set-backend-service base-url="https://myservice.azurewebsites.net" />
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

2.如果您尝试模拟响应,只需查看本教程,以便您可以了解如何返回 200 以外的其他响应:

https://learn.microsoft.com/en-us/azure/api-management/mock-api-responses?tabs=azure-portal

https://learn.microsoft.com/en-us/azure/api-management/mock-api-responses?tabs=azure-portal#enable-response-mocking

关于azure - APIM 从现有资源生成 API 不断返回 200,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73929345/

相关文章:

Azure API 管理服务集正文策略 - 修改 JSON 响应以返回某些字段

azure - 管理 Azure WebApp 部署槽和 API Manager

Azure API 管理缓存不起作用并出现错误 "Request is not cacheable, policy was not applied."

Azure API 网关 - 将 JWT token 发送到后端

java - 将 Gradle 验证为 Azure Artifacts Maven

azure - 创建独立部署时如何解决 Azure Devops Pipeline 中的目标问题

c# - Azure 和本地 TCP 连接数有很大不同

azure - OAuth2 配置问题 : AADSTS50011: The reply url specified in the request does not match the reply urls configured for the application:

python - 语法无效 : Azure CLI with Python

azure - 如何为 Azure SQL 数据库设置 UTF-8 排序规则? UTF-8 不在支持的排序规则列表中