Share via

Fetch APIM Backends in <set-variable>

Nagashree Balasundaram 741 Reputation points
2022-07-06T18:05:18.93+00:00

Is it possible to read the backend url defined under Backends using the id into a variable using the set-variable policy?

Azure API Management
Azure API Management

An Azure service that provides a hybrid, multi-cloud management platform for APIs.

0 comments No comments

Answer accepted by question author

MuthuKumaranMurugaachari-MSFT 22,451 Reputation points Moderator
2022-07-07T15:08:56.483+00:00

@Nagashree Balasundaram Thank you for reaching out to Microsoft Q&A. Based on your statement, you are setting up a backend URL using a policy like "<set-backend-service backend-id="test-Backend" />" and would like to assign it to a variable using a set-variable policy.

set-variable policy can assign a value either a string literal or using a policy expression (https://learn.microsoft.com/en-us/azure/api-management/api-management-advanced-policies#set-variable). The policy expression has access only to context variable (docs: https://learn.microsoft.com/en-us/azure/api-management/api-management-policy-expressions#ContextVariables share the full info about the fields) and hence we cannot directly read backend URL specified using backend id.

I tested with the sample policy below and I was able to retrieve the backend URL in the outbound processing using Context.Request.Url. Note, set-backend-service policy is evaluated during backend processing and hence you cannot perform the same in inbound processing to get new backend URL as it is not evaluated yet.

218646-image.png

218648-image.png

I hope this helps in answering your question. Feel free to reach out if any questions.

Please 'Accept as answer' and ‘Upvote’ if it helped so that it can help others in the community looking for help on similar topics.

Was this answer helpful?

1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Sujithreddy Komma 581 Reputation points Microsoft Employee
    2022-07-07T05:26:40.113+00:00

    Hi NagaShree,

    i doubt you have straight forward way to do that

    There is rest api to get the based on the ID. try the below

    https://learn.microsoft.com/en-us/rest/api/apimanagement/current-ga/backend/get#code-try-0

    you might need to use a send request policy to this and also the token and fetch it

    Thanks,
    Sujith.

    Was this answer helpful?


Your answer

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