'How to prevent Set header policy from executing after inbound?

I'm building an Azure Function app that is being exposed via Azure API Management. We need to know the path of the URL before it's redirected by API Management. We're currently doing that by setting the original URL in the header with an inbound policy, like this:

<policies>
<inbound>
    <base />
    <set-backend-service id="apim-generated-policy" backend-id="myfunctionapp" />
    <set-header name="x-original-url" exists-action="append">
        <value>@(context.Request.OriginalUrl.Path.ToString())</value>
    </set-header>
</inbound>
<backend>
    <base />
</backend>
<outbound>
    <base />
</outbound>
<on-error>
    <base />
</on-error>
</policies>

When testing the API in API Management, I can see the header is being set correctly, but in the logs of my function app, the header value is being overwritten with the request URL after the redirect (i.e. /api/endpoint instead of /api-manager/endpoint).

I'm guessing the policy is getting executed again after handing off to the function app, but I can't seem to figure out why. Does anyone have any experience doing this or have a better solution for sending the original URL to the function app so it can be logged?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source