为什么 Set-Body 政策无法识别我的 POST body?

Why does Set-Body policy not recognize my POST body?

我正在尝试将 Azure(api 管理)中的 API 端点连接到后端服务。但是,set-body 策略无法识别我的 JSON body,因此不会为后端调用转换它。

我已经为 "Liquid" 和 "None" 模板尝试了我能想到的所有迭代。微软文档是无用的,因为即使 "liquid" 模板在文档中被 capitalized 而它需要小写。即使是每个人都指向的 Deep Dive 文章也具有误导性 and/or 已过时。

我曾经能够使用液体模板获得 {{context.Request.OriginalUrl}} 工作参考,但我似乎无法获得 {{body.json}} 工作参考

这是我在该部分中的政策(纯粹是为了测试 - 这对我正在做的事情没有用):

<set-body template="liquid">
    Calling User Agent: {{context.Request.OriginalUrl}}
</set-body>

这是我必须尝试阅读 json body(通过 POST)的示例:

<set-body template="liquid">{{body}}</set-body>

我已经尝试了几次迭代和输入,如下所示:

<set-body template="liquid">{{body.json}}</set-body>

像这样穿过 body 时:

{"json":"this is an example body"}

无论我做什么,这都是我在测试调用后在跟踪中看到的:

set-body (0.069 ms)
{
    "input": null,
    "output": ""
} 

我显然愿意使用 "none" 模板,但我 运行 遇到了同样的问题。文档是错误的 - 如果我 copy/paste 示例:

<set-body>@(context.Body.As<String>())</set-body>

我收到如下错误:

One or more fields contain incorrect values:
Error in element 'set-body' on line 32, column 10: 'IProxyRequestContext' does not contain a definition for 'Body' and no extension method 'Body' accepting a first argument of type 'IProxyRequestContext' could be found (are you missing a using directive or an assembly reference?)

当我确实让它没有错误时,它 returns 相同的 "output":"" 输出。

为了能够在液体模板中将 body 作为 object 访问,您必须将 Content-Type header 设置为 application/jsondocs.
中所述 如果您的请求已经发送了这个 header,那么它应该可以在不设置它的情况下工作。

inbound 部分中的此类政策将保证其按预期工作

<set-header name="Content-Type" exists-action="override">
    <value>application/json</value>
</set-header>
<set-body template="liquid">{{body.json}}"}</set-body>

至于通过 context 变量访问它,您必须按照 docs 中提到的 context.Request.Body.As<string>() 访问它,所以像这样

<set-body>@(context.Request.Body.As<string>())</set-body>

set-body官方参考好像没有你说的问题
您指的是其他文档吗?如果它在 docs.microsoft.com,你可以在每个文档的末尾打开一个问题。