azure - 液态体内返回应对政策

标签 azure azure-api-management

有谁知道在使用 策略时如何从 语句内部访问请求正文? 当我这样做时, body 似乎是空的:

<inbound>
    <base />
    <set-header name="Content-Type" exists-action="override">
        <value>application/json</value>
    </set-header>
    <return-response>
        <set-status code="200" reason="OK" />
        <set-header name="Content-Type" exists-action="override">
            <value>application/json</value>
        </set-header>
        <set-body template="liquid">
            {
            "name":"{{body.firstname}}"
            }
            
        </set-body>
    </return-response>
</inbound>

请求正文是:

{"firstname" : "John Doe"}`

预期结果是:

{"name":"John Doe"}

结果是:

{"name":""}

最佳答案

Microsoft 支持人员告诉我该策略 return-response does not support set-body 与液体模板。

您可以通过在 return-response 中使用正文之前修改正文来解决此问题。

入境政策:

<inbound>
    <base />
    <set-header name="Content-Type" exists-action="override">
        <value>application/json</value>
    </set-header>
    <set-body template="liquid">
{
   "name":"{{body.firstname}}"
}
    </set-body>
    <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 body = context.Request.Body.As<string>(true);
            return body;
        }</set-body>
    </return-response>
</inbound>

请求:

POST https://rfqapiservicey27itmeb4cf7q.azure-api.net/sample/liquid HTTP/1.1
Host: rfqapiservicey27itmeb4cf7q.azure-api.net
Content-Type: application/json

{"firstname" : "John Doe"}

回应:

HTTP/1.1 200 OK
content-length: 35
content-type: application/json
date: Mon, 09 Jan 2023 04:21:51 GMT
request-context: appId=cid-v1:a10dc7c9-c354-40a2-acf3-1401681f7808
vary: Origin

{
    "name": "John Doe"
}

enter image description here

关于azure - 液态体内返回应对政策,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75051458/

相关文章:

asp.net-web-api - Net Web API 发布到 Azure API 管理服务后出现 500 错误

azure - 我可以在 Azure API 管理中使用多个身份验证提供程序吗?

azure - 无法使用 Azure APIM 交互式门户向后端 API 发送测试请求

azure - 如何备份 Azure 自定义托管镜像

php - 使用 php 的 Azure 共享访问签名错误

c# - Linq 中的 DocumentDb 空间距离返回奇怪的结果

azure - 无法使用应用服务上的 Azure MSI 访问 Key Vault

azure - 在 Azure 中使用 terraform 创建 kubernetes 集群后,如何自动对其进行身份验证?

Azure API 管理服务不会将 header 中的客户端证书传递到后端

azure-api-management - Azure APIM无法从Function App创建API