Route to different Web Service URL in same API?

FabulousFab 21 Reputation points
2021-05-11T07:48:00.3+00:00

Dear all

I'm pretty new to Azure API Management and just managed to start a flow through it. However, I would also like to start other flows without creating a new API for each of them. Ideally, I would have 1 API calling different Web Service URL's based on some rules/routing/subscriptions.

Is that somehow possible and if so, could you give me a hint?

Thank you and kind regards,

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,115 questions
Azure Health Data Services
Azure Health Data Services
An Azure offering that provides a suite of purpose-built technologies for protected health information in the cloud.
161 questions
0 comments No comments
{count} votes

Accepted answer
  1. Pramod Valavala 20,626 Reputation points Microsoft Employee
    2021-05-12T06:06:33.75+00:00

    @FabulousFab You can add multiple operations under the same API (at different paths) and leverage policies to forward requests to different web services.

    95931-image.png

    At the minimum, you would need to use the set-backend-service policy for each operation. Prefer using backends instead of URLs directly.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Faraz Ahmed SH Shaikh 1 Reputation point
    2021-06-01T10:02:23.16+00:00

    I think you are looking to orchestrate request and response from different services within a single call, such that response values from previous calls can be used as input values for subsequent calls. If this is the case then you can follow below

    <inbound>
    <base />

        <set-backend-service base-url="http://services.eg" />
        <rewrite-uri template="/server/rest/services/0/query" />
        <set-query-parameter name="f" exists-action="append">
            <value>json</value>
        </set-query-parameter>
        <set-query-parameter name="rtrElement1" exists-action="append">
            <value>true</value>
        </set-query-parameter>
        <set-query-parameter name="where" exists-action="append">
            <value>@("some string" + context.Request.OriginalUrl.Query.GetValueOrDefault(....))</value>
        </set-query-parameter>
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
        <choose>
            <when condition="@( ((IResponse)context.Response).Body.As<JObject>(true)["ftr"].Count().ToString() == "0") ">
                <return-response>
                    <set-status code="204" reason="No catchment area found" />
                    <set-header name="Content-Type" exists-action="append">
                        <value>application/json</value>
                    </set-header>
                    <set-body>No data found</set-body>
                </return-response>
            </when>
            <otherwise>
                <!-- Retrieving the coordinates in the Variables “xfd” and “yfd” -->
                <set-variable name="xfd" value="@{var x = context.Response.Body?.As<JObject>(true); return x["ftr"][0]["sName"]["x"].ToString(); }" />
                <set-variable name="yfd" value="@{ var y = context.Response.Body?.As<JObject>(false); return y["ftr"][0]["sName"]["y"].ToString(); }" />
                <!-- Sending the request to the service 2 with the X and Y coordinates returned from the previous response -->
                <send-request mode="new" response-variable-name="hcNameRsp" timeout="30" ignore-error="true">
                    <set-url>@($"http://s..)</set-url>
                    <set-method>GET</set-method>
                </send-request>
                <!-- Retrieving the response from the send request Policy stored in variable : hcNameRsp -->
                <return-response>
                    <set-status code="200" reason="OK" />
                    <set-header name="Content-Type" exists-action="override">
                        <value>application/json</value>
                    </set-header>
                    <set-body>@{                
                            var rsp = ((IResponse)context.Variables["hcNameRsp"]).Body.As<JObject>();
                            return rsp["ftr"][0]["attributes"]["NAME"].ToString();                
                        }</set-body>
                </return-response>
            </otherwise>
        </choose>
    </outbound>
    <on-error>
        <base />
    
        <return-response>
            <set-status code="400" reason="Failed to execute query." />
            <set-header name="Content-Type" exists-action="append">
                <value>application/json</value>
            </set-header>
        </return-response>
    </on-error>
    

    Hope it helps

    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.