[英]Accessing context.Request.Body in APIM policy removes request body
我正在尝试从 JSON 正文中提取特定属性并将其添加为 header 值。 这应该是直截了当的:
<set-header name="x-bp-currency" exists-action="override" >
<value>@((string)context.Request.Body.As<JObject>()["currency"])</value>
</set-header>
但是,我看到当我这样做时,请求正文在转发到后端 URL 之前被删除。我设法缩小了 context.Request.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>
没有关于此的好文档,但如果您访问 context.Request.Body,则无法在策略中进一步访问它,并且对于向前移动到后端的请求似乎是这样。
对此没有很好的文档,但我觉得很奇怪,我的谷歌搜索没有看到其他人有类似的问题。
在 set-header 中指定 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.