Home > database >  How do I construct this url rewrite policy in Azure APIM?
How do I construct this url rewrite policy in Azure APIM?

Time:08-15

In Azure APIM, looking for a url rewrite policy (I think), that looks for a certain string and constructs the backend url based on that string.

Example1:
Converts this
enter image description here

Operation policy:

<policies>
    <inbound>
        <base />
        <set-variable name="RegexLocation" value="\/locations\/[^\/]*(\/[a-zA-Z0-9-?=#_]*)" />
        <set-variable name="remainingUrl" value="@{
            string pattern = context.Variables.GetValueOrDefault<string>("RegexLocation");

            var regex = new Regex(pattern);
            var match = regex.Match(context.Request.OriginalUrl.Path.ToString());

            if (match.Success)
            {  
              return match.Groups[1].Value;;
            }       
            else
            {
                return string.Empty;
            }

        }" />
        <choose>
            <when condition="@(context.Request.MatchedParameters.GetValueOrDefault("locationid", "") == "usa")">
                <set-backend-service base-url="https://usa.com" />
            </when>
            <when condition="@(context.Request.MatchedParameters.GetValueOrDefault("locationid", "") == "canada")">
                <set-backend-service base-url="https://canada.com" />
            </when>
        </choose>
        <rewrite-uri template="@(context.Variables.GetValueOrDefault<string>("remainingUrl"))" copy-unmatched-params="true" />
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

The choose policy is used to specify the backend.
The policy rewrite-uri adds the renaming path and query

Output from trace for request:
GET https://rfqapiservicey27itmeb4cf7q.azure-api.net/abc/locations/usa/lorem?ipsum=1

The new backend URL is:
https://usa.com/lorem?ipsum=1

{
  "traceEntries": {
    "inbound": [
      {
        "source": "set-variable",
        "timestamp": "2022-08-13T07:08:45.5496188Z",
        "elapsed": "00:00:00.0003989",
        "data": {
          "message": "Context variable was successfully set.",
          "name": "RegexLocation",
          "value": "\\/locations\\/[^\\/]*(\\/[a-zA-Z0-9-?=#_]*)"
        }
      },
      {
        "source": "set-variable",
        "timestamp": "2022-08-13T07:08:45.5496188Z",
        "elapsed": "00:00:00.0004445",
        "data": {
          "message": "Expression was successfully evaluated.",
          "expression": "\n            string pattern = context.Variables.GetValueOrDefault<string>(\"RegexLocation\");\n\n            var regex = new Regex(pattern);\n            var match = regex.Match(context.Request.OriginalUrl.Path.ToString());\n\n            if (match.Success)\n            {  \n              return match.Groups[1].Value;;\n            }       \n            else\n            {\n                return string.Empty;\n            }\n\n        ",
          "value": "/lorem"
        }
      },
      {
        "source": "set-variable",
        "timestamp": "2022-08-13T07:08:45.5496188Z",
        "elapsed": "00:00:00.0004465",
        "data": {
          "message": "Context variable was successfully set.",
          "name": "remainingUrl",
          "value": "/lorem"
        }
      },
      {
        "source": "choose",
        "timestamp": "2022-08-13T07:08:45.5496188Z",
        "elapsed": "00:00:00.0004581",
        "data": {
          "message": "Expression was successfully evaluated.",
          "expression": "context.Request.MatchedParameters.GetValueOrDefault(\"locationid\", \"\") == \"usa\"",
          "value": true
        }
      },
      {
        "source": "set-backend-service",
        "timestamp": "2022-08-13T07:08:45.5496188Z",
        "elapsed": "00:00:00.0004690",
        "data": {
          "message": "Backend service URL was changed.",
          "oldBackendServiceUrl": "https://rfqapiservicey27itmeb4cf7q.azure-api.net/abc",
          "newBackendServiceUrl": "https://usa.com/",
          "request": { "url": "https://usa.com/locations/usa/lorem?ipsum=1" }
        }
      },
      {
        "source": "rewrite-uri",
        "timestamp": "2022-08-13T07:08:45.5496188Z",
        "elapsed": "00:00:00.0004757",
        "data": {
          "message": "Expression was successfully evaluated.",
          "expression": "context.Variables.GetValueOrDefault<string>(\"remainingUrl\")",
          "value": "/lorem"
        }
      },
      {
        "source": "rewrite-uri",
        "timestamp": "2022-08-13T07:08:45.5496188Z",
        "elapsed": "00:00:00.0005407",
        "data": {
          "message": "Updated request URL per specified rewrite template.",
          "request": { "url": "https://usa.com/lorem?ipsum=1" }
        }
      }
    ],
    "backend": [
      {
        "source": "forward-request",
        "timestamp": "2022-08-13T07:08:45.5496188Z",
        "elapsed": "00:00:00.0007386",
        "data": {
          "message": "Request is being forwarded to the backend service. Timeout set to 300 seconds",
          "request": {
            "method": "GET",
            "url": "https://usa.com/lorem?ipsum=1"
          }
        }
      }
    ]
  }
}
  • Related