'Azure APIM - Multiple API calls in single request (Send Request or Alternative approach)

Currently i am working on APIM where i have setup two apis

  • Generate PDF API (Appservice 1)
  • Email PDF API (Appservice 2)

in the UI, the user clicks Generate & Email API. At this point i need to call the generated pdf api and email pdf api. Also i don't want to expose the email API to UI.

What i tried in APIM is first called GeneratePDF API and inside that i called Email PDF API using .

The issue here is throws internal server error. but the API directly runs perfectly okay.

I need to know

  • Is my approach is okay for my functionality?
  • How do i execute emailing API from Generate API?


Solution 1:[1]

it should be perfectly possible to do that at APIM level using send-request policy: https://docs.microsoft.com/en-us/azure/api-management/api-management-sample-send-request. Make sure to use API test console in Azure portal while you experiment with it as it has built in trace functionality that greatly helps to understand why call may fail.

Solution 2:[2]

Yes, this is possible by using APIM orchestration policy. Basically, a nested send-request policy.

The process

  • Send the pdf request
  • Check for 200 or send an error
  • Send email request with pdf info

Look at the sample script. The hard part here is to determine the specification for your JSON document. That should be done in the set-body policy.

<send-request mode="new" response-variable-name="pdfRespose" ignore-error="false">
            <set-url>API-HERE</set-url>
            <set-method>POST</set-method>  
            <set-body>@{    
                // any transformation here or remove to use request document
            }</set-body>         
        </send-request>
        <choose>
            <!-- Check 200 status code and run the next step -->
            <when condition="@(context.Response.StatusCode < 400)">
                <!-- Send email request with required parameters-->
                <send-request mode="new" response-variable-name="emailREsponse" ignore-error="false">
                    <set-url>API-HERE</set-url>
                    <set-method>POST</set-method>
                    
                    <set-body>@{    
                            var document = (JObject)context.Variables["pdfRespose"];                            
                            return document?.ToString();                                                  
                        }</set-body>
                </send-request>
                <choose>
                    <!-- Check 200 status -->
                    <when condition="@(context.Response.StatusCode < 400)">
                        <return-response>
                            <set-status code="@(context.Response.StatusCode)" />
                            <set-body>@{
                            // send response and status code
                            }</set-body>
                        </return-response>
                    </when>
                </choose>
            </when>
            <otherwise>
                <return-response>
                    <set-status code="@(context.Response.StatusCode)" reason="Error" />                                   
                </return-response>
            </otherwise>
        </choose>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vitaliy Kurokhtin
Solution 2 ozkary