jkhan_prog
jkhan_prog

Reputation: 43

Azure API Management Policy- "rewrite-url" policy

I am trying to create a policy, that will pull the insurer ID out of the input body and put it in the URL as shown in the picture below.
Trying to clean up request body and reconstruct URL so the request can successfuly post to our approve endpoint

Expected Result:
Expected Result

https://apimanagement.test.com/consto-123/api/relationship/approve/{id}

Request body:

{
    "insurer_name": "Tony",
    "insurer_id": "12345",
    "comments": "This is test"
}

Upvotes: 2

Views: 568

Answers (1)

Markus Meyer
Markus Meyer

Reputation: 3967

You have to extract the insurer_id from the request-body and store it as a variable:

<set-variable name="insurerId" value="@{
    var body = context.Request.Body.As<JObject>(true);
    return body["insurer_id"].Value<string>();
}" />

Afterwards you can use rewrite-uri with this variable and forward the request to the blackened.

The complete policy:

<policies>
    <inbound>
        <base />
        <set-variable name="insurerId" value="@{
            var body = context.Request.Body.As<JObject>(true);
            return body["insurer_id"].Value<string>();
        }" />
        <rewrite-uri template="@("/api/relationship/approve/" + context.Variables.GetValueOrDefault<string>("insurerId"))" copy-unmatched-params="false" />
        <set-backend-service base-url="https://rfqapiservicey27itmeb4cf7q.azure-api.net" />
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

Upvotes: 1

Related Questions