Azure APIM URL rewrite with Dynamic Path from Payload

Avula, Akhila 0 Reputation points
2025-06-02T04:57:25.5+00:00

I need to rewrite URL with a value from payload.
Below code is not working.
Error:
rewrite-uri (0.122 ms) { "messages": [ null, "Variable variables.dynamicValue has no value.", "Variable variables.dynamicValue has no value." ] }

Able to fetch value from payload using set-variable.

set-variable (0.002 ms) { "message": "Context variable was successfully set.", "name": "dynamicValue", "value": "1234" }

<policies>
    <inbound>
        <base />
        <!-- Extract value from the payload -->
        <set-variable name="dynamicValue" value="@{
            var requestBody = context.Request.Body.As<JObject>();
            return requestBody.GetValue('ValuationID');
        }" />

        <!-- Rewrite the URI dynamically using the extracted value -->
        <rewrite-uri template="/newpath/@{variables.dynamicValue}" />
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. RithwikBojja 3,210 Reputation points Microsoft External Staff Moderator
    2025-06-03T04:41:40.05+00:00

    Hi @Avula, Akhila,

    Below is my api and policy which takes body payload:

    API:

    {
        "openapi": "3.0.1",
        "info": {
            "title": "TestAPi",
            "description": "API to test dynamic URL rewriting in APIM",
            "version": "1.0"
        },
        "servers": [{
            "url": "https://testapirith.azure-api.net/anything"
        }],
        "paths": {
            "/valuation": {
                "get": {
                    "summary": "Get valuation by ValuationID",
                    "description": "Get valuation by ValuationID",
                    "operationId": "GetValuation",
                    "responses": {
                        "200": {
                            "description": "Success"
                        }
                    }
                }
            }
        },
        "components": {
            "securitySchemes": {
                "apiKeyHeader": {
                    "type": "apiKey",
                    "name": "Ocp-Apim-Subscription-Key",
                    "in": "header"
                },
                "apiKeyQuery": {
                    "type": "apiKey",
                    "name": "subscription-key",
                    "in": "query"
                }
            }
        },
        "security": [{
            "apiKeyHeader": []
        }, {
            "apiKeyQuery": []
        }]
    }
    

    Policy:

    <policies>
        <inbound>
            <base />
            <set-variable name="rithtest" value="@((string)context.Request.OriginalUrl.Query.GetValueOrDefault("ValuationID"))" />
            <rewrite-uri template="/anything/@(context.Variables["rithtest"])" />
        </inbound>
        <backend>
            <base />
        </backend>
        <outbound>
            <base />
        </outbound>
        <on-error>
            <base />
        </on-error>
    </policies>
    

    Output:

    image


    If this answer was helpful, please click "Accept the answer" and mark Yes, as this can help other community members.

    enter image description here

    If you have any other questions or are still experiencing issues, feel free to ask in the "comments" section, and I'd be happy to help.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.