Share via

Dynamically execute Azure APIM Policies based on the URL

Sai Abhinav Tata 0 Reputation points
Mar 19, 2024, 7:48 PM

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.
2,358 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. MayankBargali-MSFT 70,886 Reputation points
    Mar 20, 2024, 12:02 PM

    @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.

    1 person found this answer helpful.

  2. Sai Abhinav Tata 0 Reputation points
    Apr 17, 2024, 4:59 AM

    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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.