简体   繁体   中英

Azure API Management Policy(Rewrite URI)- Template Paramets and Query Parameters

Hi I am still in progress of getting Everything to know about Azure API Management(APIM)

I require some help on doing a rewrite uri policy. I was able to extract information from payload and change the initial request URL. Have extracted the insurer_id from body and passed it to my backend -> mybackend.com/api/relationship/deny/{insurer_id}

I have a current payload:

{
  "insurer_id": "22112",
  "insurer_name": "Steve Rogers",
  "status_code: [ "Deny",
  ],
  "additionalComments": "This is a test"
}

I was able to do that as per my code below.

But I want to be able to also extract another key from my payload which is the "status_code" and pass it as part of the backend, and change the initial request URL

For example: https://mybackend.com/api/relationship/deny/{insurer_id}?denyReason={status_code} Turns into https://mybackend.com/api/relationship/deny/22112?denyReason=Deny

Here is an attempt of the code which is currently running into errors:

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

Any help on this would be much appreciated.

Thank you.

A query string consists of a key-value pair:

https://example.com/over/there?name=ferret

name is the key
ferret is the value for it

In your example, there's no key:

? + denyReason=context.Variables.GetValueOrDefault<string>("status_code")
and also the ? is a string. So it has to be put between quotation marks.

Fixed with sample query-key status_code :

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

Complete policy:

<policies>
    <inbound>
        <base />
        <set-variable name="insurerId" value="@{
            var body = context.Request.Body.As<JObject>(true);
            return body["insurer_id"].Value<string>();
        }" />
        <set-variable name="status_code" value="@{
                var body = context.Request.Body.As<JObject>(true);
                return body["status_code"].Value<string>();
         }" />
        <rewrite-uri template="@("/api/relationship/deny/" + context.Variables.GetValueOrDefault<string>("insurerId") + "?statuscode=" + context.Variables.GetValueOrDefault<string>("status_code"))" copy-unmatched-params="false" />
        <set-backend-service base-url="https://testbackend.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