[英]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.