1

I am new to Azure API Management. I am constructing a policy that will change the initial request url.How can I extract the insurer_id from body and pass it to my backend.

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

mybackend.com/api/relationship/approve/{insurer_id}

I know I have to extract the id as JObject, store it in the variable and do a rewrite-uri . But I am not sure, how I can go about implementing this in the Azure Policy. A small example would be helpful.

Thank you

Markus Meyer
  • 3,327
  • 10
  • 22
  • 35
  • This is maybe helpful: https://learn.microsoft.com/en-us/azure/api-management/api-management-advanced-policies#ForwardRequest – Ace Sep 04 '22 at 22:12
  • https://mattruma.com/adventures-with-azure-api-management-add-json-property-in-set-body/ -this page also gives related example – Anand Sowmithiran Sep 05 '22 at 07:00

1 Answers1

1

You have to read the insurer_id from request body and set it to a variable.

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

This variable can be used in rewrite-uri:

<rewrite-uri template="@("/api/relationship/approve/" + context.Variables.GetValueOrDefault<string>("insurerId") copy-unmatched-params="false" />

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://mybackend.com" />
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>
Markus Meyer
  • 3,327
  • 10
  • 22
  • 35