简体   繁体   中英

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:
预期结果

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

Request body:

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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