question

FabianAckeret-3306 avatar image
0 Votes"
FabianAckeret-3306 asked FarazAhmedSHShaikh-0800 edited

Route to different Web Service URL in same API?

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-managementazure-api-fhir
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

PramodValavala-MSFT avatar image
1 Vote"
PramodValavala-MSFT answered

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



image.png (57.7 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

FarazAhmedSHShaikh-0800 avatar image
0 Votes"
FarazAhmedSHShaikh-0800 answered FarazAhmedSHShaikh-0800 edited

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


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.