Dynamically execute Azure APIM Policies based on the URL

Abhinav Tata 0 Reputation points
2024-03-19T19:48:25.94+00:00

Hi Team,

I have a very peculiar doubt and am currently struggling to understand how it will flow in Azure APIM Policy.

I have my API URL as : https://xyz.api.net/passthrough

I have two operations in this API -

1.) /ws

2.) /rest

Now the requirement is, the /rest operation with endpoint as /rest would be appended with either /rest**/important** or /rest**/notimportant** and based upon this suffix in the URL, policies in the Azure APIM would have to be executed which translates to different backend URL being hit.

How can I achieve this in one operation ?

I tried writing up policies in Azure APIM with <when> conditions however, since the operation has /rest as the endpoint, if I hit https://xyz.api.net/passthrough/rest/important, I get a 404 error. (Obviously since this is not hosted or available but rather https://xyz.api.net/passthrough/rest is hosted)

How can I achieve this dynamic URL mapping thingy and how can I retrieve this dynamic value after /rest/ and validate or compare in the policy ?

(Also, I think creating 2 different operations simplifies this whole ordeal isn't it ?)

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
1,959 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. MayankBargali-MSFT 70,016 Reputation points
    2024-03-20T12:02:20.7733333+00:00

    @Abhinav Tata Thanks for reaching out.

    You can either define the Template parameters in the frontend which will accept any string value as below and leverage the rewrite-uri policy to route to different backend URLs based on your input url for your name parameter

    User's image

            <rewrite-uri template="@{
                var url = context.Request.OriginalUrl.Path;
                if (url.Contains("/important"))
                {
                    return url.Replace("/rest/important", "/important-backend-url");
                }
                else if (url.Contains("/notimportant"))
                {
                    return url.Replace("/rest/notimportant", "/notimportant-backend-url");
                }
                else
                {
                    return url;
                }
            }" />
    

    Yes, alternatively you can also define two different operation to route to different backend url.

    Let me know if you have any queries or concerns.

    Follow Up query resolution:

    You have to use /rest/* in my template parameter to dynamically have any number of sub-entities being passed as request. Accordingly, you parsed the necessary portion out of context.Request.Url.Path to your variable and used it for your case.

    This was the answer - using /rest**/***

    Please 'Accept Answer' if it helped so that it can help others in the community looking for help on similar topics.


  2. Abhinav Tata 0 Reputation points
    2024-04-17T04:59:51.4466667+00:00

    I think I wasn't clear on my requirement but nevertheless, I got it figured out.

    I had to use /rest/* in my template parameter to dynamically have any number of sub-entities being passed as request. Accordingly, I parsed the necessary portion out of context.Request.Url.Path to my variable and used it for my case.

    This was the answer - using /rest**/***

    0 comments No comments