简体   繁体   English

在 APIM 策略中访问 context.Request.Body 会删除请求正文

[英]Accessing context.Request.Body in APIM policy removes request body

I'm trying to extract a specific attribute from the JSON body and add that as a header value.我正在尝试从 JSON 正文中提取特定属性并将其添加为 header 值。 This should be straight forward as such:这应该是直截了当的:

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

But, I see that when I do this, the request-body gets removed before it is forwarded to the backend URL. I've managed to narrow it down that the context.Request.Body is causing the issue.但是,我看到当我这样做时,请求正文在转发到后端 URL 之前被删除。我设法缩小了 context.Request.Body 导致问题的范围。 If we add a hardcoded value, then the request-body is still sent to the backend.如果我们添加一个硬编码值,那么请求主体仍然会发送到后端。

Example:例子:

This keeps the original request-body and forwards to the backend:这将保留原始请求主体并转发到后端:

   <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>

This removes the request-body (content-length: 0).这将删除请求正文(内容长度: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>

Even just adding the whole request-body to a variable causes the request-body to be removed (either as JObject or string):即使只是将整个请求主体添加到变量中也会导致请求主体被删除(作为 JObject 或字符串):

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

or或者

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

Not that good documentation around this, but if you access context.Request.Body, it cannot be accessed again further in the policy, and it seems that this is true for the request moving forward to the backend.没有关于此的好文档,但如果您访问 context.Request.Body,则无法在策略中进一步访问它,并且对于向前移动到后端的请求似乎是这样。

There was no good documentation on this , but I find it strange that my google searches did not see others with similar issues. 对此没有很好的文档,但我觉得很奇怪,我的谷歌搜索没有看到其他人有类似的问题。

The way to specify preserveContent: true in the set-header is as such:在 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM