访问上下文.APIM策略中的Request.Body删除请求正文



我正试图从JSON主体中提取一个特定的属性,并将其添加为头值。这应该是直接的:

<set-header name="x-bp-currency" exists-action="override" >
<value>@((string)context.Request.Body.As<JObject>()["currency"])</value>
</set-header>

但是,我看到当我这样做时,在将请求体转发到后端URL之前,它会被删除。我已经设法缩小了它的范围。请求。问题由Body引起。如果我们添加了一个硬编码的值,那么请求主体仍然被发送到后端。

示例:

这将保留原始请求主体并转发到后端:

<inbound>
<base />
<set-header name="x-bp-currency" exists-action="override">
<value>test</value>
</set-header>
<set-backend-service base-url="https://webhook.site/xxxxx" />
</inbound>

这将删除请求正文(内容长度:0(。

<inbound>
<base />
<set-header name="x-bp-currency" exists-action="override" >
<value>@((string)context.Request.Body.As<JObject>()["currency"])</value>
</set-header>
<set-backend-service base-url="https://webhook.site/xxxxx" />
</inbound>

即使只是将整个请求体添加到变量中,也会导致请求体被删除(作为JObject或字符串(:

<inbound>
<base />
<set-variable name="test" value="@(context.Request.Body.As<JObject>())" />
<set-backend-service base-url="https://webhook.site/xxxxx" />
</inbound>

<inbound>
<base />
<set-variable name="test" value="@(context.Request.Body.As<string>())" />
<set-backend-service base-url="https://webhook.site/xxxx" />
</inbound>

这方面的文档不是很好,但如果您访问上下文的话。Request.Body,在策略中无法再次访问它,而且对于向前移动到后端的请求似乎也是如此。

这方面没有很好的文档,但我觉得奇怪的是,我的谷歌搜索没有看到其他有类似问题的人。

在set头中指定preserveContent:true的方法如下:

<inbound>
<base />
<set-header name="x-bp-currency" exists-action="override">
<value>@((string)context.Request.Body.As<JObject>(preserveContent: true)["currency"])</value>
</set-header>
<set-backend-service base-url="https://webhook.site/xxxxx" />
</inbound>

最新更新